Python String Methods Cheat Sheet
All built-in Python string methods with examples — searching, splitting, formatting, testing, encoding, and more. 40+ methods at a glance.
Search & Find
| Method | Returns | Description |
|---|---|---|
| s.find(sub) | int | Index of first occurrence, -1 if not found |
| s.rfind(sub) | int | Index of last occurrence, -1 if not found |
| s.index(sub) | int | Like find() but raises ValueError if not found |
| s.count(sub) | int | Count non-overlapping occurrences of sub |
| s.startswith(p) | bool | True if string starts with prefix p |
| s.endswith(p) | bool | True if string ends with suffix p |
| sub in s | bool | True if sub is a substring of s |
Transform
| Method | Returns | Description |
|---|---|---|
| s.upper() | str | Convert to uppercase |
| s.lower() | str | Convert to lowercase |
| s.title() | str | Title Case (first letter of each word capitalized) |
| s.capitalize() | str | First char uppercase, rest lowercase |
| s.swapcase() | str | Swap upper/lowercase of every character |
| s.replace(old, new) | str | Replace all occurrences of old with new |
| s.replace(old, new, n) | str | Replace first n occurrences only |
| s.translate(table) | str | Map chars via translation table (use str.maketrans) |
| s.expandtabs(n) | str | Replace tabs with n spaces (default 8) |
Split & Join
| Method | Returns | Description |
|---|---|---|
| s.split(sep) | list | Split on separator (default: whitespace) |
| s.split(sep, n) | list | Split at most n times |
| s.rsplit(sep, n) | list | Split from right at most n times |
| s.splitlines() | list | Split on line boundaries (\\n, \\r\\n, \\r) |
| s.partition(sep) | tuple | Split into (before, sep, after) at first sep |
| sep.join(iterable) | str | Join items with sep: ", ".join(["a","b"]) → "a, b" |
Strip & Pad
| Method | Returns | Description |
|---|---|---|
| s.strip() | str | Remove leading and trailing whitespace |
| s.lstrip() | str | Remove leading whitespace (left side) |
| s.rstrip() | str | Remove trailing whitespace (right side) |
| s.strip("xyz") | str | Strip specific characters instead of whitespace |
| s.removeprefix(p) | str | Remove prefix p if present (Python 3.9+) |
| s.removesuffix(s) | str | Remove suffix s if present (Python 3.9+) |
| s.center(w, fill) | str | Center in width w, padded with fill char |
| s.ljust(w, fill) | str | Left-justify in width w |
| s.rjust(w, fill) | str | Right-justify in width w |
| s.zfill(w) | str | Zero-pad to width w: "42".zfill(5) → "00042" |
Test Methods (is...)
| Method | Returns | Description |
|---|---|---|
| s.isalpha() | bool | All characters are alphabetic |
| s.isdigit() | bool | All characters are digits |
| s.isnumeric() | bool | All characters are numeric (broader than isdigit) |
| s.isalnum() | bool | All characters are alphanumeric |
| s.isspace() | bool | All characters are whitespace |
| s.islower() | bool | All cased characters are lowercase |
| s.isupper() | bool | All cased characters are uppercase |
| s.istitle() | bool | String is titlecased |
| s.isidentifier() | bool | Valid Python identifier |
| s.isprintable() | bool | All characters are printable |
Format & Encode
| Method / Syntax | Description |
|---|---|
| f"Hello {name}" | f-string (Python 3.6+) — fastest formatting |
| "Hello {}".format(x) | str.format() positional |
| "{name}".format_map(d) | Format using a mapping (dict-like) |
| f"{val:.2f}" | Format float with 2 decimal places |
| f"{val:>10}" | Right-align in 10 chars |
| f"{val:,}" | Add thousands separator: 1,000,000 |
| s.encode("utf-8") | Encode string to bytes |
| b.decode("utf-8") | Decode bytes to string |