0Pricing
Python Academy · Lesson

Format Specifiers

Control width, precision, and alignment.

Format Specifiers is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The format specifier

Inside an f-string or str.format, a colon introduces a format specifier that controls how a value is displayed: {value:spec}.

pi = 3.14159
print(f'{pi:.2f}')

Controlling decimal places

.Nf formats a float with exactly N digits after the decimal point, rounding as needed.

value = 2.0 / 3.0
print(f'{value:.3f}')
print(f'{value:.0f}')

Setting a minimum width

A number before any type code sets the minimum field width. Shorter values are padded with spaces so columns line up.

for n in [1, 42, 999]:
    print(f'[{n:5}]')

Aligning left, right, center

Alignment flags: < left, > right, ^ center. They come right after the colon and before the width.

word = 'hi'
print(f'[{word:<8}]')
print(f'[{word:>8}]')
print(f'[{word:^8}]')

Custom fill characters

Place any character before the alignment flag to use it as padding instead of spaces.

print(f'{42:*^10}')
print(f'{7:0>5}')

Width and precision together

Combine width and precision: {value:10.2f} means at least 10 characters wide with 2 decimal places.

price = 19.5
print(f'[{price:10.2f}]')

Thousands separators

A comma in the spec groups digits into thousands, making large numbers readable.

big = 1234567
print(f'{big:,}')

Percentages

The % type multiplies by 100 and appends a percent sign, with optional precision.

ratio = 0.847
print(f'{ratio:.1%}')

Integer bases

Type codes change the base: b binary, o octal, x hex (lowercase), X hex (uppercase).

n = 255
print(f'{n:b}')
print(f'{n:o}')
print(f'{n:x}')
print(f'{n:X}')

Scientific notation

The e type shows numbers in scientific notation, with precision controlling the mantissa digits.

n = 123456.789
print(f'{n:.2e}')

Dynamic width with variables

You can compute the width or precision from a variable by nesting braces inside the spec.

w = 10
val = 3.14159
print(f'[{val:{w}.2f}]')

Quick Check

What does f'{3.14159:.2f}' produce?

Recap: Format Specifiers

Key specifiers after the colon:

  • .Nf sets decimal places, a leading number sets width.
  • < > ^ control alignment, with an optional fill character before them.
  • , groups thousands, % shows percentages.
  • b o x X e change number representation, and nested braces allow dynamic width.
for name, val in [('apple', 1.5), ('fig', 22.0)]:
    print(f'{name:<8}{val:>8.2f}')

Frequently asked questions

Is the “Format Specifiers” lesson free?

Yes — the full text of “Format Specifiers” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Format Specifiers”?

Control width, precision, and alignment. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Format Specifiers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. f-string Expressions
  2. Format Specifiers
  3. Format Mini-Language
  4. Template Strings and str.format
← Back to Python Academy