name = "Alice"age = 25value = 3.14159# Insert variables f"Hello {name}, you are {age} years old"# Format the variables f"Pi is approximately {value:.2f}"f"Percentage: {0.85:.1%}"f"Scientific: {1234:.2e}"
2. Common Format Specifiers
Format
Description
Example
Output
:.2f
2 decimal places
f"{3.14159:.2f}"
3.14
:.0f
No decimals
f"{3.14159:.0f}"
3
:d
Integer
f"{42:d}"
42
:.1%
Percentage
f"{0.85:.1%}"
85.0%
:.2e
Scientific notation
f"{1234:.2e}"
1.23e+03
:>10
Right align, width 10
f"{'hi':>10}"
hi
:<10
Left align, width 10
f"{'hi':<10}"
hi
:^10
Center align, width 10
f"{'hi':^10}"
hi
3. Padding and Alignment
# Zero padding for numbersf"{42:05d}" # "00042"f"{3.14:08.2f}" # "00003.14"# String alignmentf"{'text':>10}" # " text"f"{'text':<10}" # "text "f"{'text':^10}" # " text "