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/0xprefixes 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).
- type —
d b o x f e g % setc.
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
0enables sign-aware zero padding:f"{-7:05d}"->-0007. gswitches between fixed and scientific automatically;nis locale-aware.- F-strings and
str.formatshare this identical mini-language. - Precision on strings truncates characters, not bytes:
f"{'hello':.3}"→'hel'. - The
!r,!s, and!aconversion flags go before the colon (not inside the spec) and callrepr(),str(), orascii()on the value first.