Apr-01-2018, 12:32 PM
(Apr-01-2018, 11:15 AM)Nearrivers Wrote: Also is f-string like printNot like print which is a function,
f-string
is string formatting
with super power The history of string formatting.
city = 'Oslo' description = 'Windy' temperature = 25 # Old way don't use it print('%s is currently %s and temperature is %dC' % (city, description, temperature)) # Python 2.6(10-year ago) we get .format() print('{0} is currently {1} and temperature is {2}C'.format(city, description, temperature)) # Python 3.6 we get f-string print(f'{city} is currently {description} and temperature is {temperature}C')All output the same:
Output:Oslo is currently Windy and temperature is 25C
Some super power.>>> # f-strings support any Python expressions inside the curly braces >>> name = 'f-string' >>> print(f"My cool string formatting is called {name.upper():*^20}") My cool string formatting is called ******F-STRING****** >>> cost = 99.75999 >>> finance = 50000 >>> print(f'Toltal cost {cost + finance:.2f}') Toltal cost 50099.76 >>> for word in 'f-strings are cool'.split(): ... print(f'{word.upper():~^20}') ... ~~~~~F-STRINGS~~~~~~ ~~~~~~~~ARE~~~~~~~~~ ~~~~~~~~COOL~~~~~~~~