2/7 String formatting evolution:
name = "John"
print("Hello %s" % name) # 1989 called
print("Hello {}".format(name)) # Old but reliable
print(f"Hello {name}") # Modern elegance
Each prints "Hello John" but f-strings are clearer.
#Python
name = "John"
print("Hello %s" % name) # 1989 called
print("Hello {}".format(name)) # Old but reliable
print(f"Hello {name}") # Modern elegance
Each prints "Hello John" but f-strings are clearer.
#Python
Comments
price = 10.99
f"${price:.2f}" # $10.99
f"{price:>10}" # Right align
f"{price:,.2f}" # Add commas
Format with style, no calculator needed.
#PythonTips
name = "python"
f"{name.title()}" # Python
f"{len(name)}" # 6
f"{name[:2].upper()}" # PY
They run code right inside your strings!
#LearnPython
x = 42
y = "hello"
f"{x=}, {y=}" # x=42, y='hello'
It's like print() but smarter. Your debugging sessions just got an upgrade.
#PythonTips
Quotes inside quotes
f'He said "Hello"' # Works
f"She said 'Hi'" # Also works
Need curly braces?
f"A curly brace: {{" # Shows: A curly brace: {
#Python
• Use f-strings for readability
• .format() for dynamic templates
• %-formatting for legacy code
• All tools have their place
Choose the right tool for your task.
Welcome to modern Python! 🐍
#PythonTips #CodeNewbie