Stop 02 · Variables, Data Types & OperatorsTopics · 7/8

f-Strings & String Formatting

Part of the Python Roadmap

In short

Receipts, reports, messages: most output mixes fixed text with changing values. f-strings let you write the sentence once and drop the values straight in, with exact control over decimals, commas and alignment.

f-strings (f"Hello {name}") put variables and expressions straight into text, and format specifiers like {price:.2f} or {count:,} control decimals, padding and thousands separators.

The jaadu presses a rubber stamp onto a printed form with empty boxes, filling each box neatly in turn.

Think of it like this

A fill-in-the-blanks form

A printed exam admit card has blanks: Name ____, Roll No ____, Centre ____. The office fills each blank with the right detail, neatly in the box. An f-string is that card: the {curly braces} are the blanks, and Python writes each value in, formatted to fit.

Watch it happen

How f"Total: ₹{price * qty:,.2f}" is built

Find { }
Evaluate
Format spec
Insert
Final string

1/5Python spots the replacement field inside the braces. Everything outside stays as plain text.

The big ideas

What you need to know

Any expression in braces

f"{name.title()} scored {marks / 5:.1f}%" works. Python 3.12 even allows the same quote type inside the braces.

Number formats

.2f for two decimals, , for 1,000 separators, % to show 0.125 as 12.5%, and 05d to pad 7 to 00007.

Alignment and width

{item:<15} left-aligns in 15 characters, {qty:>4} right-aligns, {title:^20} centres. Perfect for neat tables.

The debug = trick

f"{total = }" prints total = 256.5, showing both the expression and its value. Great for quick debugging.

See it in code

Walk through the code

python
1item = "Masala Dosa"2price = 85.53qty = 34total = price * qty5 6print(f"{item} x {qty} = ₹{total}")     # Masala Dosa x 3 = ₹256.57print(f"Total: ₹{total:.2f}")            # Total: ₹256.508print(f"Downloads: {2500000:,}")         # Downloads: 2,500,0009print(f"Discount: {0.125:.1%}")          # Discount: 12.5%10print(f"|{item:<15}|{qty:>4}|")          # |Masala Dosa    |   3|11print(f"{qty * 2 = }")                   # qty * 2 = 6

1/5Plain replacement: each value is converted to text and dropped in.

The f before the opening quote turns on the magic. Without it, the braces print literally.

Save yourself the pain

Common mistakes

Oops

Forgetting the f, so print("Hello {name}") shows the braces literally.

Do this instead

Put f right before the quote: print(f"Hello {name}").

Oops

Building text with + and str(), like "Total: " + str(total) + " rupees".

Do this instead

Use f"Total: {total} rupees". It is shorter, faster and easier to read.

Oops

Rounding with round() before printing and still seeing 256.5 instead of 256.50.

Do this instead

round() changes the number, not the display. Use :.2f in the f-string to always show two decimals.

Your mission

Print a canteen receipt

~25 min

Quick check

What does f"{7:03d}" produce?

Go deeper

  1. 1Print a price with exactly two decimal places using an f-string.
  2. 2Use {value:>10} and {value:<10} to align columns in a small table.
  3. 3Use f"{x=}" while debugging to print both the name and value.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge