With Python version 3.6, a new string formatting mechanism was introduced. This feature is named the Formatted String Literal or f-string. In this lesson, you’ll learn how to do variable interpolation with f-strings. With this feature, you can specify a variable name directly within an f-string literal, and Python will replace the name with the corresponding value:
>>> n =20
>>> m = 25
>>> prod = n * m
>>> prod
500
>>> print('The product of', n, 'and', m, 'is', prod)
The product of 20 and 25 is 500
>>> print(f'The product of {n} and {m} is {prod}')
The product of 20 and 25 is 500
>>> print(f'The product of {n} and {m} is {n * m}')
The product of 20 and 25 is 500
>>> f''
''
>>> F''
''
>>> var = 'Bark'
>>> print(f'A dog says {var}!')
A dog says Bark!
>>> print(F"A dog says {var}!")
A dog says Bark!
>>> print(f'''A dog says {var}!''')
A dog says Bark!
>>> print(f'A dog says {var}!")
File "<input>", line 1
print(f'A dog says {var}!")
^
SyntaxError: EOL while scanning string literal
Here are some resources on f-strings:
Minh Pham on March 2, 2020
Hi Christopher,
I would like to know how to get the Python Shell you are using in this video to teach. I got Python from MS Appstore, try to configure IDLE but could not get the “hints” poping up every time I type something.
Thanks Minh