The basic f-string
name = "Ana"
age = 30
print(f"{name} is {age} years old")
# Ana is 30 years old
The braces are replaced by the value of each expression, making the result easy to read and write.
Run expressions inside the braces
price = 8
print(f"Total: {price * 3}") # Total: 24
print(f"Upper: {name.upper()}") # Upper: ANA
You are not limited to plain variables — any valid Python expression works inside the braces.
Control number formatting
pi = 3.14159
print(f"{pi:.2f}") # 3.14
print(f"{1000000:,}") # 1,000,000
A colon after the expression starts a format spec: .2f fixes two decimal places and , adds thousands separators.
Common mistakes
- Forgetting the f. Without it,
"Hi {name}"prints the braces literally instead of the value. - Reusing the same quote inside. If the f-string uses double quotes, use single quotes for keys inside the braces, or vice versa.
Frequently asked questions
Are f-strings faster than .format()?
Yes, f-strings are generally faster and easier to read than str.format() or the older % style, so they are the recommended choice in modern Python.
Which Python versions support f-strings?
Python 3.6 and later. On older versions you would fall back to .format().
Want to practise f-strings hands-on? Our free Python course includes an in-browser editor.