TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 6 of 22 · By Ethan Cole · Updated November 21, 2026

Python f-strings: Format Numbers, Text and Tables

Gluing values into text with + quickly gets messy, because every number has to be converted with str() first. Formatted string literals, or f-strings, solve this. Put an f before the opening quote and write any expression inside curly braces; Python evaluates it and inserts the result.

Expressions inside braces

f"{name} bought {items} books" reads almost like the sentence it produces. The braces can hold any expression: arithmetic, method calls, indexing. Keep them short, though; if the expression is long, compute it on the line before and give it a name.

Format specifiers

After the expression you can add a colon and a format specifier that controls how the value is shown:

  • :.2f shows a float with exactly two decimals, rounding as needed.
  • :, adds thousands separators; combine as :,.2f.
  • :.1% multiplies by 100 and adds a percent sign.
  • :<8, :>5 and :^10 pad to a width with left, right or center alignment. That is how you line up columns of a text table.
  • :05d pads an integer with leading zeros to width 5.
Python 3 · editable

Expected output

Ava bought 3 books for $41.50
Item    |  Qty
Pens    |   12
1,234,567.89
25.6%
items * 2 = 6

The last line uses the = specifier, added in Python 3.8. It prints the expression text followed by its value, which is very handy when debugging. To show a literal brace, double it: f"{{" produces {.

Other formatting styles

You will meet two older styles in existing code. "{} and {}".format(a, b) uses the same specifiers as f-strings. The % operator, as in "%d items" % 3, is the oldest style and still works. For new code, f-strings are the clearest and also the fastest option.

Common mistakes

  • Forgetting the f prefix, so the braces are printed literally.
  • Using the same quote character inside the braces as around the string. Before Python 3.12 that was a syntax error; use the other quote type.
  • Applying :.2f to text. Format specifiers must match the value's type, or Python raises ValueError.

Summary

  • f"...{expr}..." inserts the value of any expression.
  • :.2f, :,, :% control number display; <, >, ^ with a width align text.
  • {expr=} prints the expression and its value for debugging.

Quiz: f-strings and formatting

1. What does f"{3.14159:.2f}" produce?

.2f rounds to two digits after the decimal point.

2. Which f-string prints 1,500,000?

The comma option inserts thousands separators.

3. What is printed by print("{x}") when x = 5?

Without the f prefix it is an ordinary string, so the braces are printed as typed.

4. What does f"{'ok':>5}" produce?

>5 right-aligns the text in a field five characters wide.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.