Python f-strings explained with examples
October 27, 2026 · 1 min read
An f-string is a string with an f before the opening quote. Anything inside {} is worked out by Python and put into the text, so you can drop in variables and even calculations.
Example: a receipt line
item = "mug"
price = 8.50
quantity = 3
print(f"{quantity} x {item} at {price} each")
print(f"Total: {price * quantity}")
print(f"Total: {price * quantity:.2f}")
print(f"{item.upper()} costs ${price:.2f}")
Output:
3 x mug at 8.5 each
Total: 25.5
Total: 25.50
MUG costs $8.50
How it works
- The
fbefore the quote turns the string into an f-string. Without it, the braces are just text. {quantity}and{item}are replaced by the values of those variables.- Inside the braces you can write any expression, like
price * quantityoritem.upper(). Python calculates it first, then inserts the result. :.2fafter the expression is a format. It shows a number with exactly 2 digits after the decimal point, so25.5becomes25.50. This is handy for money.- The
$in the last line is plain text. Only{}is special.
You can use f-strings with numbers and text alike. Python converts the value to a string for you, so you don't need str(). That is why they are easier to read than joining pieces with +, where a number would need str() first.
Common mistakes
Forgetting the f. The braces then print literally:
price = 8.50
print("Total: {price}")
Output:
Total: {price}
Fix: write print(f"Total: {price}").
Forgetting the braces. Names outside {} are just text:
print(f"Total: price * quantity")
This prints Total: price * quantity. Fix: put the expression inside braces, like f"Total: {price * quantity}".
Takeaway
Put f before the quote, put variables or expressions in {}, and add :.2f when you want a price with two decimals.