QuickDevGuide

Home › Python

Strings & f-strings — Python

Python strings are immutable Unicode sequences. Single, double and triple quotes are all valid; triple quotes span multiple lines. Formatting with f-strings (3.6+) f-strings evaluate expressions inline and support a format spec after a colon: Spec Result f"{pi:.2f}" 3.14 f"{n:,}" 1,234,567 f"{0.256:.1%}" 25.6% f"{name:>10}" / :<10 / :^10 right / left / centre align f"{n:08.3f}" zero-padded f"{value!r}" repr() form f"{x=}" x=42 — great for debugging (3.8+) Slicing s[start:stop:step] works on any sequence: s[::-1] reverses, s[-3:] takes the last three characters. Common methods split, join, strip, replace, startswith, find, upper/lower/title, isdigit, zfill, removeprefix/removesuffix (3.9+).

Key Takeaways