What are f-strings in Python?

An f-string (formatted string) is a string that starts with f and can hold variables and expressions inside curly braces { }. They were added in Python 3.6 and are the easiest way to format text.

name = "Robin"
age = 20
price = 49.5

print(f"{name} is {age} years old")
print(f"Next year: {age + 1}")
print(f"Total: {price:.2f}")

Output:

Tutorials Class - Output Window

Robin is 20 years old
Next year: 21
Total: 49.50

:.2f shows the number with two digits after the decimal point. F-strings are shorter and easier to read than the older % formatting or format().


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.