f-strings and Formatting

Putting values inside strings, and the small format language that controls how each one is printed.

Overview

The prefix does the work

f"{name} scored {score}"

The f before the quote is what turns braces into slots. Without it you get the literal characters. Inside the braces goes any expression — a variable, a method call, arithmetic — evaluated at that point and inserted.

That is the whole idea, and it is why f-strings replaced everything before them: the value appears in the source where it appears in the output. Concatenation with + scatters the sentence across quotes and plus signs, and % and .format() push the values to the end, away from the slots they fill.

fstrings.py

fstrings.py Python 3
Output

                    

formatting_compare.py

formatting_compare.py Python 3
Output

                    

Worth knowing

The f prefix is required. Without it the braces are just braces.
:.2f fixes two decimal places, :, adds thousands separators, :>8 right-aligns in eight columns.
Formatting changes what is shown, never the value itself.
f"{x = }" prints both the expression and its value - the fastest debug print there is.

f-strings and Formatting: A Practical Guide

An f-string puts values inside a string where they will appear, and a small format language after each colon controls exactly how they are printed.

The format spec

After a colon comes a compact language for presentation:

  • :.2f — two decimal places
  • :, — thousands separators
  • :>8 — right-align in eight columns; < left, ^ centre
  • :03d — pad to three digits with zeros
  • :.1% — show as a percentage with one decimal

They combine: {price:>10.2f} right-aligns in ten columns with two decimals. That is how you get a table whose numbers line up without counting spaces by hand.

Formatting is not rounding

f"{value:.2f}" changes how the number is displayed. The variable is untouched, still carrying every digit it had. This matters when you print a rounded figure and then keep computing with the original: the printed total and the computed total can differ, and the program is not wrong — the display was never the value.

The debugging shortcut

f"{total = }"

prints total = 42 — the expression and its value. It works for any expression, so f"{len(items) = }" prints both the call and its result. It is the fastest way to instrument a function, and it removes the classic mistake of printing a label that no longer matches the variable beside it.

Which to use

Use f-strings. % formatting and .format() still work and you will meet them in older code, but they exist now mainly to be read, not written. The one place .format() still earns its keep is when the template is stored separately from the values — a message in a config file, for instance — because an f-string is evaluated where it is written.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does `f"{2/3:.2f}"` produce?

  2. What is `{price:>10.2f}` doing?

  3. You write `"{name} scored"` with no f prefix. What prints?

Cheat sheet

f-strings and Formatting

An f-string puts values inside a string where they will appear, and a small format language after each colon controls exactly how they are printed.

PYTHON · vizlearn.in/python/f_strings_and_formatting.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.