0% found this document useful (0 votes)
28 views

Python From Scratch: Website - Linkedin - Youtube

This document discusses different methods for string formatting in Python, including concatenation, format() method, and f-strings. It shows examples of formatting strings to output reservoir pressure information for a given field and pressure value. Various string formatting methods like format() and f-strings allow easy insertion of variables into strings compared to concatenation. The order of arguments in format() is important to match the placeholders in the string.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Python From Scratch: Website - Linkedin - Youtube

This document discusses different methods for string formatting in Python, including concatenation, format() method, and f-strings. It shows examples of formatting strings to output reservoir pressure information for a given field and pressure value. Various string formatting methods like format() and f-strings allow easy insertion of variables into strings compared to concatenation. The order of arguments in format() is important to match the placeholders in the string.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like