← Cheat Sheets
Python Strings and Formatting
String methods and f-strings.
# Python Strings and Formatting
String methods and f-strings.
```python
s = " Hello, World "
s.strip(); s.lower(); s.upper(); s.title()
s.split(","); ",".join(["a", "b"])
s.replace("l", "L"); s.startswith("He"); s.endswith("d"); s.find("o")
"abc"[::-1] # reverse: "cba"
name, pi = "Ann", 3.14159
f"Hello {name}" # f-string
f"{pi:.2f}" # 3.14
f"{1234567:,}" # 1,234,567
f"{5:03d}" # 005
f"{0.256:.1%}" # 25.6%
f"{name:>10}" f"{name:<10}" f"{name:^10}" # align right, left, center
f"{x=}" # debug: x=5
```