Uploaded by Kelvin Joseph

Slides+-+String+Formatting

advertisement
Formatting Strings
● The new way of formatting strings (Python 3.6+) is to use formatted string literals or
f-strings which is a string literal that is prefixed with ‘f’ of ‘F’.
f-strings are evaluated at run time
These strings may contain replacement fields, which are expressions delimited by pairs of
curly braces { }.
>>> price = 3.4
>>> f'The price is:{price}'
'The price is:3.4'
● Another way of formatting strings is to use the format( ) method.
>>> miles = 23.2
>>> 'The value in miles is {}'.format(miles)
'The value in miles is 23.2'
Master Python Programming
By Andrei Dumitrescu
Download