Format Specifiers in Python
Python provides format specifiers for formatting strings, numbers, and other data types. These specifiers can be used with f-strings (f""
), the format()
method, or %
formatting.
1. Integer Formatting
Format | Description | Example |
---|---|---|
%d or {} |
Integer (decimal) | print(f"{100:d}") → 100 |
%o or {:#o} |
Octal representation | print(f"{100:o}") → 144 |
%x or {:#x} |
Hexadecimal (lowercase) | print(f"{100:x}") → 64 |
%X or {:#X} |
Hexadecimal (uppercase) | print(f"{100:X}") → 64 |
%b or {:#b} |
Binary representation | print(f"{100:b}") → 1100100 |
Examples
num = 100
print(f"Decimal: {num:d}")
print(f"Binary: {num:b}")
print(f"Octal: {num:o}")
print(f"Hexadecimal: {num:X}")
Output:
Decimal: 100
Binary: 1100100
Octal: 144
Hexadecimal: 64
2. Floating-Point Formatting
Format | Description | Example |
---|---|---|
%f or {:.nf} |
Floating point with n decimal places |
print(f"{3.14159:.2f}") → 3.14 |
%e or {:.ne} |
Scientific notation (lowercase) | print(f"{3.14159:.2e}") → 3.14e+00 |
%E or {:.nE} |
Scientific notation (uppercase) | print(f"{3.14159:.2E}") → 3.14E+00 |
%g or {:.ng} |
Auto chooses %f or %e based on value |
print(f"{3.14159:.3g}") → 3.14 |
Examples
pi = 3.14159265358979
print(f"Default: {pi}")
print(f"2 Decimal Places: {pi:.2f}")
print(f"Scientific Notation: {pi:.2e}")
print(f"Compact: {pi:.3g}")
Output:
Default: 3.14159265358979
2 Decimal Places: 3.14
Scientific Notation: 3.14e+00
Compact: 3.14
3. String Formatting
Format | Description | Example |
---|---|---|
%s or {} |
String | print(f"{'Python'}") → Python |
%r or {!r} |
Raw string (repr) | print(f"{'Hello!':!r}") → 'Hello!' |
%c |
Character from ASCII | print(f"{65:c}") → A |
Examples
text = "Hello"
print(f"Normal: {text}")
print(f"Repr: {text!r}")
print(f"ASCII Character: {65:c}")
Output:
Normal: Hello
Repr: 'Hello'
ASCII Character: A
4. Aligning and Padding
Format | Description | Example |
---|---|---|
{:<n} |
Left-align | print(f"{'Hi':<10}") → 'Hi ' |
{:>n} |
Right-align | print(f"{'Hi':>10}") → ' Hi' |
{:^n} |
Center-align | print(f"{'Hi':^10}") → ' Hi ' |
{:0n} |
Zero-padding | print(f"{42:05}") → 00042 |
Examples
name = "Python"
print(f"Left: {name:<10}-End")
print(f"Right: {name:>10}-End")
print(f"Center: {name:^10}-End")
print(f"Zero-Padded: {42:05}")
Output:
Left: Python -End
Right: Python-End
Center: Python -End
Zero-Padded: 00042
5. Percentage Formatting
Format | Description | Example |
---|---|---|
% |
Converts to percentage | print(f"{0.85:.0%}") → 85% |
Example
discount = 0.25
print(f"Discount: {discount:.0%}")
Output:
Discount: 25%
6. Comma Separators for Large Numbers
Format | Description | Example |
---|---|---|
:, |
Adds comma separators | print(f"{1000000:,}") → 1,000,000 |
Example
big_number = 1000000
print(f"Formatted Number: {big_number:,}")
Output:
Formatted Number: 1,000,000
7. Custom Number Base Formatting
Format | Description | Example |
---|---|---|
{:#b} |
Binary with 0b prefix |
print(f"{10:#b}") → 0b1010 |
{:#o} |
Octal with 0o prefix |
print(f"{10:#o}") → 0o12 |
{:#x} |
Hexadecimal with 0x prefix |
print(f"{10:#x}") → 0xa |
Conclusion
Python format specifiers enhance readability and make data representation flexible. Mastering them is crucial for clean, readable code! 🚀
Leave a Reply