A Python f-string starts with f and puts expressions inside braces. With name = "Hana", f"Hello, {name}" produces "Hello, Hana". A format specifier after a colon controls how the value looks. For instance, {score:.1f} displays one digit after the decimal point.
name = "Hana"
score = 8.25
message = f"{name}: {score:.1f} points"
assert message == "Hana: 8.2 points"
.1f displays one digit after the decimal point. The number 8.25 is exactly halfway between 8.2 and 8.3, so Python formats the tie with the even last digit, 2. The variable score still holds 8.25. Other decimal values can land a little to either side of a tie after converting to a binary float.
Format decimal places, percentages, and thousands
Choose precision according to the information you want to convey. f"{seconds:.2f}" shows two decimal places and may be appropriate for a short duration. It can imply false precision if the measurement is only reliable to the nearest second. For percentages, f"{share:.1%}" multiplies the value by 100 for display and adds a percent sign. Pass a fraction like 0.375 if you want 37.5%.
Thousands separators help in a large count. f"{12750:,}" yields 12,750. If someone needs to enter it back into a program, keep the underlying value separately. A comma in a formatted string may require correct quoting in CSV or regional interpretation in a UI. Use a data serializer for an actual data export.
Give a console report predictable columns
A minimum width can make short labels easier to scan in a plain terminal.
room = "Lab"
items = 14
line = f"{room:<8}{items:>4}"
assert line == "Lab 14"
<8 left-aligns the label in at least eight character positions. >4 right-aligns the count in at least four. A long value can exceed the width, so this does not enforce a maximum. Some characters also use different numbers of terminal columns than Python's string length suggests. Review the actual display if data includes wide characters or combining marks.
Distinguish visible text from its representation
Use !r in an f-string when you want Python's representation of a value. That can reveal a trailing newline, outer spaces, or the difference between 7 and "7". For ordinary messages the default representation is often easier to read. Diagnostics should also avoid printing actual secrets or data the recipient is not meant to see.
Double braces to show a literal brace. With items = 14, f"{{item}} = {items}" produces {item} = 14. Python evaluates the expressions you write inside replacement fields. Braces that arrive inside a substituted string are ordinary text.
Choose the format that fits the output destination
F-strings suit short messages with values available in the current scope. Keep expensive work out of the braces when assigning it first would simplify debugging or prevent repeated calls. For JSON, use a JSON encoder. For SQL queries, use parameter binding. Formatting an ordinary Python value into a message cannot substitute for the destination's escaping, data rules, and boundaries.
In automated tests, check both the numeric calculation and the displayed text when formatting matters. Try zero, a value that rounds near a boundary, and a label longer than the nominal width. If the same formatted value is shown in multiple places, define its display precision once and have the views call that same policy.