F-strings provide a new, concise way to format strings in Python 3.6. They allow embedding expressions inside curly braces within a string that starts with f. This allows printing variables directly into a string without needing string formatting or .format() methods. F-strings are faster than older string formatting methods and make multiline strings easier to read by allowing expressions across multiple lines. Documentation on f-strings can be found in the Python docs glossary under "formatted string literal".
5. How we format strings before ...
>>> name = “Bart”
>>> print("Hello, %s." % name)
Hello, Bart.
6. More than one argument?
>>> name = “Bart”
>>> age = 10
>>> print("Hello, %s. You’re %s." % name, age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
7. More than one argument?
>>> name = “Bart”
>>> age = 10
>>> print("Hello, %s. You’re %s." % (name, age))
Hello, Bart. You’re 10.
8. str.format
>>> name = “Bart”
>>> age = 10
>>> print("Hello, {name}. You’re {age}.".format(name=name,age=age))
Hello, Bart. You’re 10.
9. F-string
>>> name = “Bart”
>>> age = 10
>>> print(f"Hello, {name}. You’re {age}.")
Hello, Bart. You’re 10.