You have three pieces — category="Coffee", amount=4.50, tag="cash". The output you want is "Coffee: $4.50 [cash]". Can you do it with one f-string?
I think so. Each placeholder becomes a slot in the template, and I can put literal characters like : and [ ] between them.
Exactly right. An f-string doesn't care how many placeholders you use — one or ten, same rules:
f"{category}: ${amount:.2f}"So I just extend that idea to three placeholders, with literal bracket characters [ and ] in between?
Exactly. Only { and } are special in an f-string — everything else is literal text. Brackets, parens, dots, colons, dollar signs — they render as typed. Stretch the template out to three slots:
result = f"{category}: ${amount:.2f} [{tag}]"What if the tag itself contains curly braces? Like someone passed in "{promo}" as the tag. Would Python try to interpret it?
It wouldn't. The {} only counts inside the template string — it's parsed once, when the f-string is built. The values that fill placeholders are treated as raw data. So tag="{promo}" renders as literal text [{promo}]. Safe by default.
So I can mix any number of variables with any literal text, and Python keeps each one in its slot. One line stays readable even with three fields.
That's the power of the template approach. You describe the shape of the output once, and each call fills it the same way. Three fields today, ten fields later — same pattern, same rules.
TL;DR: one template, many slots, each slot gets its own format spec.
{...} is independent — mix and match specs{ or } inside values| Slot | Spec | Example output |
|---|---|---|
{category} | (none) | Coffee |
{amount:.2f} | 2 decimals | 4.50 |
{tag} | (none) | cash |
For a full row: f"{category}: ${amount:.2f} [{tag}]" → Coffee: $4.50 [cash].
Write `format_expense_row(category, amount, tag)` that returns `"Coffee: $4.50 [cash]"` for `("Coffee", 4.5, "cash")`. Amount is 2 decimals; tag is wrapped in square brackets.
Tap each step for scaffolded hints.
No blank-editor panic.
You have three pieces — category="Coffee", amount=4.50, tag="cash". The output you want is "Coffee: $4.50 [cash]". Can you do it with one f-string?
I think so. Each placeholder becomes a slot in the template, and I can put literal characters like : and [ ] between them.
Exactly right. An f-string doesn't care how many placeholders you use — one or ten, same rules:
f"{category}: ${amount:.2f}"So I just extend that idea to three placeholders, with literal bracket characters [ and ] in between?
Exactly. Only { and } are special in an f-string — everything else is literal text. Brackets, parens, dots, colons, dollar signs — they render as typed. Stretch the template out to three slots:
result = f"{category}: ${amount:.2f} [{tag}]"What if the tag itself contains curly braces? Like someone passed in "{promo}" as the tag. Would Python try to interpret it?
It wouldn't. The {} only counts inside the template string — it's parsed once, when the f-string is built. The values that fill placeholders are treated as raw data. So tag="{promo}" renders as literal text [{promo}]. Safe by default.
So I can mix any number of variables with any literal text, and Python keeps each one in its slot. One line stays readable even with three fields.
That's the power of the template approach. You describe the shape of the output once, and each call fills it the same way. Three fields today, ten fields later — same pattern, same rules.
TL;DR: one template, many slots, each slot gets its own format spec.
{...} is independent — mix and match specs{ or } inside values| Slot | Spec | Example output |
|---|---|---|
{category} | (none) | Coffee |
{amount:.2f} | 2 decimals | 4.50 |
{tag} | (none) | cash |
For a full row: f"{category}: ${amount:.2f} [{tag}]" → Coffee: $4.50 [cash].