You have a category like "Food" and an amount like 12.5. You want the final output to read "Food: $12.50". How would you stitch a string together with a float?
I'd try category + ": $" + amount — but I have a feeling Python will complain that I'm mixing a string with a number.
It will. Plain + between a string and a float raises a TypeError — Python wants both sides to match. The clean fix is an f-string, a template that accepts any type inline:
result = f"{category}: ${amount}"That works, but I want exactly two decimals every time. 12.5 should render as 12.50, and 12 should render as 12.00 — consistent output no matter what the input looked like.
Good instinct. Presentation shouldn't depend on the input's precision. F-strings support format specifiers after a colon — :.2f means "float, 2 decimal places". Drop it in:
result = f"{category}: ${amount:.2f}"Does :.2f break if the amount comes in as an integer, like 20, instead of a float?
It doesn't. Python coerces 20 into 20.0 before applying .2f, so the output is 20.00. The format spec decides the shape of the output — the input type is flexible.
So one f-string handles every number cleanly — whole integers, messy decimals, even zero — all with one line?
That's the power of a clean template. One line, one shape, every row of your report lined up the same way. No branching, no edge cases.
TL;DR: f-strings drop values into a template; the spec after : controls the shape.
{var} — value as-is{var:.2f} — float, 2 decimals{var:,} — thousands separators{var:>8} — right-align in an 8-char field| Spec | Example | Output |
|---|---|---|
:.2f | f"{12.5:.2f}" | 12.50 |
:, | f"{1234:,}" | 1,234 |
:>5 | f"{7:>5}" | 7 |
Forget the f prefix and the braces stay literal — a silent bug.
Write `format_expense(category, amount)` that returns a string like `"Food: $12.50"` — always 2 decimal places, dollar sign included.
Tap each step for scaffolded hints.
No blank-editor panic.
You have a category like "Food" and an amount like 12.5. You want the final output to read "Food: $12.50". How would you stitch a string together with a float?
I'd try category + ": $" + amount — but I have a feeling Python will complain that I'm mixing a string with a number.
It will. Plain + between a string and a float raises a TypeError — Python wants both sides to match. The clean fix is an f-string, a template that accepts any type inline:
result = f"{category}: ${amount}"That works, but I want exactly two decimals every time. 12.5 should render as 12.50, and 12 should render as 12.00 — consistent output no matter what the input looked like.
Good instinct. Presentation shouldn't depend on the input's precision. F-strings support format specifiers after a colon — :.2f means "float, 2 decimal places". Drop it in:
result = f"{category}: ${amount:.2f}"Does :.2f break if the amount comes in as an integer, like 20, instead of a float?
It doesn't. Python coerces 20 into 20.0 before applying .2f, so the output is 20.00. The format spec decides the shape of the output — the input type is flexible.
So one f-string handles every number cleanly — whole integers, messy decimals, even zero — all with one line?
That's the power of a clean template. One line, one shape, every row of your report lined up the same way. No branching, no edge cases.
TL;DR: f-strings drop values into a template; the spec after : controls the shape.
{var} — value as-is{var:.2f} — float, 2 decimals{var:,} — thousands separators{var:>8} — right-align in an 8-char field| Spec | Example | Output |
|---|---|---|
:.2f | f"{12.5:.2f}" | 12.50 |
:, | f"{1234:,}" | 1,234 |
:>5 | f"{7:>5}" | 7 |
Forget the f prefix and the braces stay literal — a silent bug.