Python Basics — Format Specifiers
Format Specifiers
Format specifiers control how values are displayed — width, alignment, decimal places, number bases, and more. They work with both f-strings and str.format().
The Format Spec Mini-Language
The general syntax inside the braces is:
{value:[[fill]align][sign][#][0][width][grouping][.precision][type]}
Don’t worry about memorizing this — let’s break it down with examples.
Width and Alignment
| Syntax | Meaning | Example | Result |
|---|---|---|---|
{:10} |
Min width 10 | f"{'hi':10}" |
"hi " |
{:<10} |
Left-align | f"{'hi':<10}" |
"hi " |
{:>10} |
Right-align | f"{'hi':>10}" |
" hi" |
{:^10} |
Center | f"{'hi':^10}" |
" hi " |
Fill Characters
# Default fill is space
print(f"{'Title':^20}") # Title
# Custom fill character
print(f"{'Title':*^20}") # *******Title********
print(f"{'Title':=^20}") # =======Title========
print(f"{'Title':-^20}") # -------Title--------
print(f"{'Title':.^20}") # .......Title........
Number Formatting
Decimal Places
pi = 3.14159265
print(f"{pi:.0f}") # 3
print(f"{pi:.1f}") # 3.1
print(f"{pi:.2f}") # 3.14
print(f"{pi:.4f}") # 3.1416
print(f"{pi:.10f}") # 3.1415926500
Thousands Separator
big = 1234567890
print(f"{big:,}") # 1,234,567,890
print(f"{big:_}") # 1_234_567_890
# Combining with decimals
price = 1234567.89
print(f"${price:,.2f}") # $1,234,567.89
Percentage
rate = 0.856
print(f"{rate:%}") # 85.600000%
print(f"{rate:.1%}") # 85.6%
print(f"{rate:.0%}") # 86%
Sign Display
pos = 42
neg = -42
print(f"{pos:+}") # +42 (always show sign)
print(f"{neg:+}") # -42
print(f"{pos:-}") # 42 (only show negative sign — default)
print(f"{pos: }") # ' 42' (space for positive, minus for negative)
print(f"{neg: }") # -42
Number Bases
| Type | Meaning | Example | Result |
|---|---|---|---|
b |
Binary | f"{42:b}" |
"101010" |
o |
Octal | f"{42:o}" |
"52" |
x |
Hex (lower) | f"{255:x}" |
"ff" |
X |
Hex (upper) | f"{255:X}" |
"FF" |
d |
Decimal | f"{42:d}" |
"42" |
num = 255
print(f"Decimal: {num:d}") # Decimal: 255
print(f"Binary: {num:b}") # Binary: 11111111
print(f"Octal: {num:o}") # Octal: 377
print(f"Hex: {num:x}") # Hex: ff
print(f"Hex: {num:X}") # Hex: FF
# With prefix (#)
print(f"Binary: {num:#b}") # Binary: 0b11111111
print(f"Octal: {num:#o}") # Octal: 0o377
print(f"Hex: {num:#x}") # Hex: 0xff
Zero Padding
# Pad numbers with zeros
print(f"{42:05}") # 00042
print(f"{42:08}") # 00000042
print(f"{3.14:08.2f}") # 00003.14
# Useful for file names, IDs
for i in range(1, 4):
print(f"file_{i:03}.txt")
# file_001.txt
# file_002.txt
# file_003.txt
Scientific Notation
big = 1234567890.0
small = 0.000123
print(f"{big:e}") # 1.234568e+09
print(f"{big:.2e}") # 1.23e+09
print(f"{small:e}") # 1.230000e-04
print(f"{small:.2e}") # 1.23e-04
# General format — chooses fixed or scientific
print(f"{big:g}") # 1.23457e+09
print(f"{small:g}") # 0.000123
print(f"{3.14:g}") # 3.14
Building a Formatted Table
# Complete table example
data = [
("Alice", 95, 3.87),
("Bob", 87, 3.45),
("Charlie", 92, 3.72),
]
print(f"{'Name':<10} {'Score':>6} {'GPA':>6}")
print(f"{'-'*10} {'-'*6} {'-'*6}")
for name, score, gpa in data:
print(f"{name:<10} {score:>6} {gpa:>6.2f}")
Output:
Name Score GPA
---------- ------ ------
Alice 95 3.87
Bob 87 3.45
Charlie 92 3.72
Quick Reference
| Format | Description | Input | Output |
|---|---|---|---|
:.2f |
2 decimal places | 3.14159 |
3.14 |
:, |
Thousands separator | 1000000 |
1,000,000 |
:.1% |
Percentage (1 dec) | 0.856 |
85.6% |
:05 |
Zero-padded width 5 | 42 |
00042 |
:>10 |
Right-align width 10 | "hi" |
" hi" |
:<10 |
Left-align width 10 | "hi" |
"hi " |
:^10 |
Center width 10 | "hi" |
" hi " |
:+ |
Always show sign | 42 |
+42 |
:b |
Binary | 10 |
1010 |
:x |
Hexadecimal | 255 |
ff |
:.2e |
Scientific notation | 1234.5 |
1.23e+03 |
Summary
- Format specifiers control how values are displayed in f-strings
- Use
:.Nffor decimal places,:,for thousands separators - Use
<,>,^for alignment with optional fill characters - Use
:.N%for percentage formatting - Use
:b,:o,:xfor binary, octal, and hex - Use
:0Nfor zero-padding - Combine specifiers:
f"${price:>12,.2f}"
Next up: Practice Exercises →