DEV Community

Tony Sterlin Cantave
Tony Sterlin Cantave

Posted on Originally published at tonycantave.com

Python f Strings for Readable Reports: How I Format Small Script Output

When I ran regional delivery route operations, the most useful thing a script could do at the end of the day was print a summary a tired person could read in five seconds. Stops completed, stops late, routes still open. The data was never the hard part. The hard part was output that lined up, rounded the right way and did not make anyone squint.

I still write a lot of small Python scripts, now for my own projects and for the code samples I write in my AI evaluation work. Almost all of them end with some kind of report. Python f strings handle nearly all of that formatting for me, and the format spec language that rides along with them is worth learning properly. Here are the patterns I actually use.

The basics

An f string is a string literal with an f in front of it. Anything inside curly braces is evaluated as a Python expression.

route = "North"
stops = 42
print(f"Route {route} finished {stops} stops")
Enter fullscreen mode Exit fullscreen mode

You can put any expression in the braces, including method calls:

name = "  new haven  "
print(f"Town: {name.strip().title()}")
Enter fullscreen mode Exit fullscreen mode

Keep the expressions short. If the braces start holding logic, compute a variable first and drop that in. The string should read like the sentence it prints.

Numbers: decimals, commas and percentages

This is where f strings save the most time. After the expression, a colon starts the format spec.

total = 1234567.891
print(f"{total:,.2f}")    # 1,234,567.89

rate = 0.8734
print(f"{rate:.1%}")      # 87.3%

count = 7
print(f"{count:03d}")     # 007
Enter fullscreen mode Exit fullscreen mode

A comma adds thousands separators, .2f rounds to two decimals, and % multiplies by 100 and adds the percent sign. I use .1% for on time rates and pass rates because one decimal is enough to see a trend without pretending the data is more precise than it is.

Lining up columns

A report is much easier to scan when the columns line up. Width and alignment go in the same spec: < for left, > for right, ^ for center. The rows below are made up sample data.

rows = [
    ("North", 42, 0.952),
    ("Shoreline", 37, 0.892),
    ("Valley", 51, 0.98),
]

print(f"{'Route':<12}{'Stops':>8}{'On time':>10}")
for route, stops, rate in rows:
    print(f"{route:<12}{stops:>8}{rate:>10.1%}")
Enter fullscreen mode Exit fullscreen mode

Output:

Route          Stops   On time
North             42     95.2%
Shoreline         37     89.2%
Valley            51     98.0%
Enter fullscreen mode Exit fullscreen mode

Text goes left, numbers go right. That one habit makes most console reports readable. You can also choose a fill character, so f"{'':=<30}" prints a divider of thirty equals signs.

Widths decided at runtime

Sometimes you do not know the widest value until the script runs. You can nest braces inside the spec:

width = max(len(r[0]) for r in rows) + 2
for route, stops, rate in rows:
    print(f"{route:<{width}}{stops:>6}")
Enter fullscreen mode Exit fullscreen mode

Dates

Datetime objects accept strftime codes directly inside an f string:

from datetime import datetime

now = datetime(2026, 11, 26, 14, 5)
print(f"Report run {now:%b %d, %Y at %I:%M %p}")
# Report run Nov 26, 2026 at 02:05 PM
Enter fullscreen mode Exit fullscreen mode

That removes a separate strftime call and keeps the whole line readable.

Debugging with the equals sign

Since Python 3.8, adding = after an expression prints both the expression and its value:

late = 3
total = 42
print(f"{late=} {total=} {late / total=:.2%}")
# late=3 total=42 late / total=7.14%
Enter fullscreen mode Exit fullscreen mode

I use this constantly while writing evaluation samples. It is faster than typing labels by hand, and the label never drifts out of sync with the variable name.

repr versus str

Adding !r shows the repr of a value, which includes quotes and makes hidden whitespace visible:

value = "Valley "
print(f"[{value}] vs {value!r}")
# [Valley ] vs 'Valley '
Enter fullscreen mode Exit fullscreen mode

When a CSV import fails to match a name that looks identical on screen, !r is usually how I find the trailing space that caused it.

Things I avoid

  • Building SQL or shell commands with f strings. Use parameterized queries for SQL and argument lists for subprocess. f strings are for display, not for anything that gets executed.
  • Long expressions inside the braces. If I need a comprehension in there, it belongs on its own line.
  • Treating float formatting as money math. Rounding for display is fine, but for totals that matter I use Decimal and format the result.

A tiny report function

Putting it together, here is the shape most of my end of run summaries take:

def summary(rows):
    width = max(len(r[0]) for r in rows) + 2
    lines = [f"{'Route':<{width}}{'Stops':>7}{'On time':>10}"]
    lines.append(f"{'':=<{width + 17}}")
    for name, stops, rate in rows:
        lines.append(f"{name:<{width}}{stops:>7,}{rate:>10.1%}")
    total = sum(r[1] for r in rows)
    lines.append(f"{'Total':<{width}}{total:>7,}")
    return "\n".join(lines)

print(summary(rows))
Enter fullscreen mode Exit fullscreen mode

It is a dozen lines, it has no dependencies, and anyone can read the output without opening a spreadsheet.

That is the real point for me. Most small scripts are judged by the last ten lines they print. A few minutes spent on formatting turns a script that technically works into one people actually trust and use.

Originally published at tonycantave.com by Tony Sterlin Cantave, a Connecticut software developer.

Top comments (0)