f-string formatting styles in Python,
✅ General Syntax
f"{expression:[fill][align][width][,][.precision][type]}"
Where:
fill: character used to fill empty space (default is space)
align: <, >, ^, =
width: field width
,: comma for thousands separator
.precision: number of digits after the decimal (for floats)
type: format type code (varies by data type)
📘 1. String Formatting
Syntax Description Example Output
f"{s:<10}" Left-align 'hi' 'hi '
f"{s:>10}" Right-align 'hi' ' hi'
f"{s:^10}" Center-align 'hi' ' hi '
f"{s:*^10}" Custom fill 'hi' '****hi****'
🔢 2. Integer Formatting
Syntax Description Example Output
f"{n:d}" Decimal (default) 42 '42'
f"{n:b}" Binary 42 '101010'
f"{n:o}" Octal 42 '52'
f"{n:x}" Hex (lower) 42 '2a'
f"{n:X}" Hex (upper) 42 '2A'
f"{n:,}" Thousands separator 1000000 '1,000,000'
f"{n:>8}" Right-align width 8 42 ' 42'
f"{n:=+8}" Pad after sign -42 '- 42'
[Link] 1/5
🧮 3. Float Formatting
Syntax Description Example Output
f"{f:.2f}" 2 decimal places 3.14159 '3.14'
f"{f:10.2f}" Width 10, 2 decimals 3.14 ' 3.14'
f"{f:,.2f}" Thousands separator 1234567.89 '1,234,567.89'
f"{f:e}" Exponential 1234567.89 '1.234568e+06'
f"{f:%}" Percent 0.25 '25.000000%'
📅 4. Date and Time Formatting
Requires the datetime module.
from datetime import datetime
now = [Link]()
Syntax Description Example Output
f"{now:%Y-%m-%d}" Date 2025-04-23 '2025-04-23'
f"{now:%H:%M:%S}" Time — '13:45:10'
f"{now:%A}" Weekday name — 'Wednesday'
✅ 5. Boolean Formatting
Booleans are formatted like strings or integers:
flag = True
print(f"{flag}") # True
print(f"{int(flag)}") # 1
🛠️ 6. Other Handy Tricks
🧪 Debugging (Python 3.8+)
x = 10
print(f"{x=}") # Output: x=10
🔄 Padding Numbers
n = 7
print(f"{n:03}") # Output: 007
👀 Object Attributes
class Person:
def __init__(self, name): [Link] = name
p = Person("Alice")
print(f"{[Link]}") # Output: Alice
[Link] 2/5
🧮 Summary Table of Format Types
Type Meaning
s String
d Decimal integer
b Binary
o Octal
x Hex (lowercase)
X Hex (uppercase)
e Scientific notation
f Fixed-point
% Percentage
, Thousand separator
. Precision control
📘 1. String Formatting
s = "hi"
print(f"{s:<10} <- Left aligned") # hi
print(f"{s:>10} <- Right aligned") # hi
print(f"{s:^10} <- Center aligned") # hi
print(f"{s:*^10} <- Center with fill") # ****hi****
🔢 2. Integer Formatting
n = 42
big = 1000000
print(f"Decimal : {n:d}")
print(f"Binary : {n:b}")
print(f"Octal : {n:o}")
print(f"Hex (lower) : {n:x}")
print(f"Hex (upper) : {n:X}")
print(f"Thousands : {big:,}")
print(f"Right-align : {n:>8}")
print(f"Pad after sign: {-n:=+8}")
[Link] 3/5
🧮 3. Float Formatting
f = 3.14159
large_float = 1234567.89
percent = 0.25
print(f"2 decimals : {f:.2f}")
print(f"Width 10 + 2 dec : {f:10.2f}")
print(f"Thousands : {large_float:,.2f}")
print(f"Exponential : {large_float:e}")
print(f"Percent : {percent:%}")
📅 4. Date and Time Formatting
from datetime import datetime
now = [Link]()
print(f"Date : {now:%Y-%m-%d}")
print(f"Time : {now:%H:%M:%S}")
print(f"Weekday : {now:%A}")
✅ 5. Boolean Formatting
flag = True
print(f"Boolean as string : {flag}")
print(f"Boolean as int : {int(flag)}")
🛠️ 6. Other Handy Tricks
Debugging (Python 3.8+)
x = 10
y = 5
print(f"{x=}, {y=}, sum={x + y}")
Padding Numbers
n = 7
print(f"Zero-padded : {n:03}")
Object Attributes
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age
p = Person("Alice", 30)
print(f"Name: {[Link]}, Age: {[Link]}")
[Link] 4/5
🧮 7. Summary: All in One Table Format
print(f"{'Label':<20}{'Formatted Value'}")
print(f"{'-'*30}")
print(f"{'Decimal':<20}{42:d}")
print(f"{'Binary':<20}{42:b}")
print(f"{'Octal':<20}{42:o}")
print(f"{'Hex (lower)':<20}{42:x}")
print(f"{'Fixed-point (2)':<20}{3.14159:.2f}")
print(f"{'Percentage':<20}{0.85:%}")
print(f"{'Date':<20}{now:%Y-%m-%d}")
print(f"{'Time':<20}{now:%H:%M:%S}")
[Link] 5/5