f-strings & String Formatting
Almost every program eventually needs to weave data into text — a greeting with the user's name, a receipt with a price, a log line with a timestamp. f-strings are Python's clean, fast, and readable way to do exactly that.
Learn f-strings & String Formatting in our free Python course — a beginner-friendly interactive lesson with runnable examples, a practice exercise and a…
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll format money, percentages, tables, and dates like a professional, and you'll understand the format spec mini-language that powers it all.
An f-string (formatted string literal) is a normal string with the letter f placed right before the opening quote. Inside it, anything you wrap in curly braces {' '} is treated as Python code, evaluated, and slotted into the text.
Notice you never call str() inside an f-string — Python converts each value to text for you automatically. That single feature removes a whole category of TypeError bugs.
The same line of output, written three ways. Read each and decide which one you'd want to maintain at 2am during an outage:
The braces are not limited to bare variable names. You can do arithmetic, call methods, index into a list, or even run a small conditional — all inline:
After a value you can add a colon and a format spec : {' '} . This is where f-strings go from convenient to genuinely powerful. Money, percentages, and aligned columns all come from here.
The most common spec by far is :.2f — "show this float with exactly two digits after the decimal point", perfect for currency. The :, spec instantly makes large numbers human-readable.
Use < (left), > (right), or ^ (center) with a width to line text up into columns — the trick behind every console report:
{' '} left-aligns the name in a 12-character field; {' '} right-aligns the price in an 8-character field with 2 decimals. You can even pick the fill character: {" "} centers nothing in a 20-wide field of asterisks.
Put an = right after an expression inside the braces and Python prints both the expression text and its value. It's the fastest way to sprinkle debug prints:
No more typing the variable name twice. Reach for this any time you'd otherwise write print("x", x) .
Because {' '} means "evaluate this", you need a way to print an actual brace. The rule: double it.
Know how to read them, but write f-strings in new code.
These scrambled lines should compute a discounted price and print a clean formatted line. Reorder them so the final output is Final: $42.50 (15% off) .
Why: discount (A) needs both price (B) and rate (D) to exist first. final (C) depends on the discount, and the f-string (E) reads both final and rate . The :.0% spec turns 0.15 into 15% .
7 — the braces evaluate the full expression 3 * 2 + 1 , following normal operator precedence.
50% — the % spec multiplies by 100 and appends a percent sign; .0 means zero decimal places.
{' '} — the outer doubled braces are literal, the inner single pair interpolates n .
Build a single formatted invoice line for a product, then experiment with the format specs.
Lesson complete — you format like a pro now!
You can embed expressions, format money and percentages, align columns, use the = debug trick, and read the older .format() and % styles. f-strings will appear in nearly every Python file you ever write.
🚀 Up next: String Methods — slice, search, clean, and reshape text with Python's built-in string toolkit.
Practice quiz
What does the 'f' in f-string stand for?
- fast
- function
- formatted
- final
Answer: formatted. The f stands for 'formatted' — a formatted string literal.
Given x = 3, what does f'{x * 2 + 1}' print?
- 7
- 6
- 9
- x * 2 + 1
Answer: 7. The braces evaluate the full expression 3*2+1 = 7.
What does f'{0.5:.0%}' print?
- 0.5%
- 5%
- 0%
- 50%
Answer: 50%. The % spec multiplies by 100 and .0 means zero decimals, giving 50%.
Given n = 5, what does f'{{{n}}}' print?
- {{5}}
- {5}
- 5
- {n}
Answer: {5}. The outer doubled braces are literal and the inner pair interpolates n, giving {5}.
What does f'{1299.5:.2f}' print?
- 1299.50
- 1299.5
- 1,299.50
- 1300
Answer: 1299.50. .2f shows exactly two decimal places: 1299.50.
What does f'{9500000:,}' print?
- 9500000
- 9.5M
- 9,500,000
- 9 500 000
Answer: 9,500,000. The , spec adds thousands separators: 9,500,000.
What does f'{42:05d}' print?
- 42
- 00042
- 42000
- 42
Answer: 00042. 05d zero-pads the integer to a width of 5: 00042.
How do you print a literal single curly brace inside an f-string?
- Escape it with a backslash
- Use a backtick
- You cannot
- Double it: {{ or }}
Answer: Double it: {{ or }}. Doubling the brace ({{ or }}) produces one literal brace.
Given x = 7, what does the debug syntax f'{x=}' print?
- 7
- x=7
- x
- x = 7
Answer: x=7. The = debug form prints both the expression text and its value: x=7.
Which alignment spec right-aligns a value in a 10-character field?
- {x:<10}
- {x:^10}
- {x:>10}
- {x:10<}
Answer: {x:>10}. > means right-align; <10 left-aligns and ^10 centers.