1️⃣ Expressions Inside F-strings
Embed any valid Python expression inside {}
x, y = 10, 20
print(f"Sum: {x + y}") # Outputs: Sum: 30
2️⃣ String Formatting
Control string width, alignment, and fill characters.
name = "Alice"
print(f"{name:>10}") # Outputs: ' Alice' (right-aligned with spaces)
3️⃣ Date Formatting
Format dates without needing to call strftime.
import datetime
today = datetime.date.today()
print(f"{today:%B %d, %Y}") # Outputs: 'August 28, 2023' (for example)
4️⃣ Number Formatting
Format numbers to a specified number of decimal places.
pi = 3.141592653589793
print(f"Pi rounded: {pi:.2f}") # Outputs: Pi rounded: 3.14
5️⃣ Debugging with Variable Names
Display variables and their values conveniently.
x, y = 5, 10
print(f"{x=} {y=}") # Outputs: x=5 y=10
#strings #f-strings #stringformatting