Python Format String Reference

Decode any Python format spec and look up every fill, align, sign and type code

Interactive reference for Python's format specification mini-language used by f-strings and str.format — paste a spec like :>10.2f to decode its fill, align, sign, width, precision, and type, plus a full code table. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is Python's format specification mini-language?

It is the small grammar that controls how a value is rendered inside an f-string replacement field or str.format placeholder, written after a colon. The full form is [[fill]align][sign][#][0][width][grouping][.precision][type]. For example f'{x:>+08.2f}' right-aligns, forces a sign, zero-pads to width 8 with two decimals as a fixed-point float.

Python’s format specification mini-language controls how a value is rendered inside an f-string or str.format placeholder, written after a colon. This tool decodes any spec you paste and lists every fill, align, sign, and type code.

How it works

The full grammar is:

[[fill]align][sign][#][0][width][grouping][.precision][type]

Each component is optional and parsed left to right:

  • fill + align — a pad character and one of < (left), > (right), ^ (center), = (pad after the sign). Fill requires an explicit align character.
  • sign+ (always show), - (only negative, default), space (leading space for positives).
  • # — alternate form: 0b/0o/0x prefixes for integers, always-show decimal point for floats.
  • 0 — sign-aware zero padding (shorthand for 0=).
  • width — minimum field width.
  • grouping, or _ thousands separator.
  • .precision — digits after the decimal (floats) or max characters (strings).
  • typed b o x f e g % s etc.

The parser below splits your spec into exactly these parts and explains each.

Example

f"{1234.5:>+12,.2f}"   # '   +1,234.50'
f"{255:#06x}"           # '0x00ff'
f"{0.27:.1%}"           # '27.0%'

Real-world format specs

Aligning table columns

When printing tabular data to the terminal or a text file, alignment characters keep columns tidy:

header = f"{'Name':<20} {'Score':>8} {'Grade':^6}"
row    = f"{'Alice':<20} {97.5:>8.1f} {'A':^6}"
# Name                    Score  Grade
# Alice                    97.5    A

Currency and financial output

For currency, combine a comma grouping separator with a fixed two-decimal precision:

amount = 1234567.89
f"${amount:,.2f}"      # '$1,234,567.89'
f{amount:>15,.2f}"   # '  £1,234,567.89'  — right-aligned in 15-char field

Displaying binary, octal, and hex

The alternate # flag adds the appropriate prefix automatically:

n = 255
f"{n:b}"    # '11111111'       — binary, no prefix
f"{n:#b}"   # '0b11111111'     — binary with 0b prefix
f"{n:#010b}"# '0b11111111'     — zero-padded to 10 characters
f"{n:#x}"   # '0xff'           — hex with prefix
f"{n:#08X}" # '0x0000FF'       — uppercase hex, zero-padded

Scientific and general notation

big = 12345678.0
f"{big:e}"      # '1.234568e+07'  — scientific
f"{big:.3e}"    # '1.235e+07'     — 3 significant decimal places
f"{big:g}"      # '1.23457e+07'   — g auto-picks fixed or scientific
f"{big:.10g}"   # '12345678'      — g with enough precision stays fixed

Percentage

ratio = 0.8372
f"{ratio:.1%}"   # '83.7%'   — multiplies by 100 and appends %

Formatting datetime and custom objects

The format specification mini-language also applies to datetime objects via their __format__ method, accepting strftime-style codes after the colon:

from datetime import datetime
now = datetime.now()
f"{now:%Y-%m-%d}"       # '2026-06-21'
f"{now:%d %B %Y}"       # '21 June 2026'
f"{now:%H:%M}"          # '14:30'

Any class that implements __format__(self, spec) can accept custom spec strings. The built-in numeric and string types follow the mini-language grammar; other types interpret the spec in their own way.

Notes

  • A leading 0 enables sign-aware zero padding: f"{-7:05d}" -> -0007.
  • g switches between fixed and scientific automatically; n is locale-aware.
  • F-strings and str.format share this identical mini-language.
  • Precision on strings truncates characters, not bytes: f"{'hello':.3}"'hel'.
  • The !r, !s, and !a conversion flags go before the colon (not inside the spec) and call repr(), str(), or ascii() on the value first.