(Dec-12-2018, 12:23 AM)lga13 Wrote: can you explain what the f on this line does?
f-string
is the new string formatting(new in 3.6).format()
we have had since 26..Example.
# older way >>> first_name = "Eric" >>> age = 30 >>> print('Name is:{} Age is:{}'.format(first_name, age)) Name is:Eric Age is:30 >>> >>> # The new way >>> first_name = "Eric" >>> age = 30 >>> print(f'Name is:{first_name} Age is:{age}') Name is:Eric Age is:30
>>> print(f"Sammy has {4:4} red and {16:16}! blue balloons") Sammy has 4 red and 16! blue balloons >>> # f-strings support any Python expressions inside the curly braces >>> name = 'f-string' >>> print(f"My cool string is called {name.upper()}.") My cool string is called F-STRING. >>> a, b = 5, 7 >>> f'{a}/{b} = {a/b:.2}' '5/7 = 0.71' >>> for word in 'f-strings are awesome'.split(): ... print(f'{word.upper():~^20}') ~~~~~F-STRINGS~~~~~~ ~~~~~~~~ARE~~~~~~~~~ ~~~~~~AWESOME~~~~~~~