1/28/2021 Python for O&G Lecture 6 - String Formatting - Colaboratory
Python From Scratch
Website - https://petroleumfromscratchin.wordpress.com/
LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch
YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw
field = 'KG Basin'
press = 5000 # psi
We use string formatting because it is lot easier than just simply using concatenation
/
1/28/2021 Python for O&G Lecture 6 - String Formatting - Colaboratory
# ugly method (Concatenation)
print('Reservoir pressure is' + ' ' + str(press) + ' ' + 'psi')
Reservoir pressure is 5000 psi
# Python 3
print('Reservoir pressure is {} psi'.format(press)) # does not matter that press is an integer value
Reservoir pressure is 5000 psi
# Python 3.6
print(f'Reservoir pressure is {press} psi')
Reservoir pressure is 5000 psi
# output required: Reservoir pressure in KG Basin is 5000 psi
# ugly method
print('Reservoir pressure in' + ' ' + field + 'is' + ' ' + str(press) + ' ' + 'psi')
Reservoir pressure in KG Basinis 5000 psi
# python 3 method
print('Reservoir pressure in {} is {} psi'.format(field, press)) # order is important
Reservoir pressure in KG Basin is 5000 psi
/
1/28/2021 Python for O&G Lecture 6 - String Formatting - Colaboratory
print('Reservoir pressure in {} is {} psi'.format(press, field))
Reservoir pressure in 5000 is KG Basin psi
print('Reservoir pressure in {field_name} is {pressure} psi'.format(pressure = press, field_name = field))
Reservoir pressure in KG Basin is 5000 psi
# python 3.6 method
print(f'Reservoir pressure in {field} is {press} psi')
Reservoir pressure in KG Basin is 5000 psi
# we can also change presure to any differet values!
# python 3 method
print('Reservoir pressure in {} is {} psi'.format(field, press+50))
Reservoir pressure in KG Basin is 5050 psi
# python 3.6 method
print(f'Reservoir pressure in {field} is {press+110} psi')
Reservoir pressure in KG Basin is 5110 psi
/
1/28/2021 Python for O&G Lecture 6 - String Formatting - Colaboratory