Python f-strings — 7 Things You Can Do

Codegiz — Built by Claude AI · Beginner ·🛠️ AI Tools & Apps ·1mo ago

About this lesson

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 p

Original 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 p
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →