Python f-strings — 7 Things You Can Do
0views
C
CelesteAI
Description
An f-string is the modern way to build strings in Python. The f prefix turns the string into a template, and anything inside curly braces gets evaluated and dropped in. That much most people know. But f-strings have a much deeper format spec mini-language — number formatting, alignment, percentages, the = debugging shortcut, conversion flags, multi-line templates, and date formatting all live inside the same braces.
This tutorial walks seven things you can do with f-strings, on a tiny demo with a name, a price, and a date. Each example is one or two lines. The point is to leave you with a mental shelf of patterns to reach for the next time you build a string in Python.
What You'll Build:
- f_string_examples.py — one file, seven numbered patterns, runs in under a second.
- Pattern 1: basic interpolation — f"Hello, {name}". Drop a variable into a string.
- Pattern 2: format spec — f"${price:,.2f}" for thousand-separator + 2 decimals, f"{count:gt6}" for right-padding, f"{pct:.2%}" for percentage. Same colon-and-spec syntax as the old .format method.
- Pattern 3: expressions inside the braces — f"{items[0]}", f"{sum(items)}", f"{name.upper()}". Anything that evaluates to a value works. Method calls, indexing, arithmetic.
- Pattern 4: = for debugging (Python 3.8+) — f"{x=}" prints x=10. Saves typing the variable name twice when you're sticking print statements into code to figure out what's wrong.
- Pattern 5: !r / !s / !a conversion flags — f"{val!r}" calls repr(), !s calls str(), !a calls ascii(). The !r form is the most useful — it quotes strings, so you can tell "5" from 5 in your debug output.
- Pattern 6: multi-line f-strings — triple-quote a template, embed values across multiple lines. Useful for printing structured reports.
- Pattern 7: date and time formatting — f"{today:%Y-%m-%d}". The format spec accepts strftime codes for dates and datetimes. No more importing strftime separately.
Timestamps:
0:00 - Intro — f-strings in one minute
0:22 - Preview — seven patterns coming up
1:07 - Open f_string_examples.py in nvim
1:25 - Pattern 1 — basic interpolation
1:42 - Pattern 2 — format spec for numbers
2:05 - Pattern 3 — expressions in braces
2:25 - Pattern 4 — = for debugging
2:45 - Pattern 5 — conversion flags
3:08 - Pattern 6 — multi-line
3:30 - Pattern 7 — date formatting
3:55 - Save and run
4:10 - Walk through the output
4:30 - End screen — recap
Key Takeaways:
1. An f-string is a template — the f prefix turns the string into one. Anything inside curly braces gets evaluated and substituted in at runtime. The expression can be a variable, a method call, an index, arithmetic — anything that produces a value.
2. The format spec mini-language lives after a colon inside the braces. {price:,.2f} is comma-separator, 2 decimals. {count:gt6} is right-pad to width 6. {pct:.2%} is percentage with 2 decimals. Same syntax as the old .format method and the format() built-in.
3. The = debug shortcut (Python 3.8+) is the killer feature for sticking print statements into code. f"{x=}" prints x=10. Saves you from typing the variable name twice as both label and value. Use everywhere you'd reach for print-debugging.
4. Conversion flags — !r, !s, !a — call repr, str, and ascii respectively before formatting. !r is the most useful in practice: it quotes strings in the output, so you can tell empty strings from None and a string '5' from the number 5 in your debug logs.
5. The format spec accepts strftime codes for dates and datetimes. f"{today:%Y-%m-%d}" formats a date without importing strftime separately. Same for percentage, hex, binary, scientific notation — all the format codes from the format-spec mini-language work inside f-string braces.
📺 Deeper dive: Common Questions Ep 12 (List Comprehensions) is a similar "patterns you'll meet in real code" walk-through for another Python idiom. https://www.youtube.com/watch?v=-ZJmrY48290
This channel is run by Claude AI. Tutorials AI-produced; reviewed and published by Codegiz. Source code at codegiz.com.
#Python #FStrings #PythonTutorial #LearnPython #PythonFormatting #PythonBasics #PythonTips
---
Generated by Claude AI · part of the Common Questions in Python series