Complete reference for Python string methods: split, join, replace, strip, find, format, f-strings, and more. With examples for each method.
| Method | Description |
s.find(sub) | Return lowest index of sub or -1 if not found. "hello".find("ll") → 2 |
s.find(sub, start, end) | Search within slice s[start:end]. "hello hello".find("hello", 1) → 6 |
s.rfind(sub) | Return highest index of sub or -1. "hello hello".rfind("hello") → 6 |
s.index(sub) | Like find(), but raises ValueError if not found |
s.rindex(sub) | Like rfind(), but raises ValueError if not found |
s.count(sub) | Count non-overlapping occurrences of sub. "banana".count("a") → 3 |
s.startswith(prefix) | Return True if string starts with prefix. "hello".startswith("he") → True |
s.startswith(tuple) | Check against multiple prefixes. "hello".startswith(("he", "ha")) → True |
s.endswith(suffix) | Return True if string ends with suffix. "hello".endswith("lo") → True |
"sub" in s | Membership test. "ell" in "hello" → True |
| Method | Description |
s.replace(old, new) | Replace all occurrences. "hello".replace("l", "r") → "herro" |
s.replace(old, new, count) | Replace first count occurrences. "aaa".replace("a", "b", 2) → "bba" |
s.strip() | Remove leading and trailing whitespace. " hi ".strip() → "hi" |
s.strip(chars) | Remove specified characters from both ends. "##hi##".strip("#") → "hi" |
s.lstrip() | Remove leading whitespace only. " hi ".lstrip() → "hi " |
s.rstrip() | Remove trailing whitespace only. " hi ".rstrip() → " hi" |
s.split() | Split by whitespace into a list. "a b c".split() → ["a", "b", "c"] |
s.split(sep) | Split by separator. "a,b,c".split(",") → ["a", "b", "c"] |
s.split(sep, maxsplit) | Split at most maxsplit times. "a,b,c".split(",", 1) → ["a", "b,c"] |
s.rsplit(sep, maxsplit) | Split from the right. "a,b,c".rsplit(",", 1) → ["a,b", "c"] |
s.splitlines() | Split by line breaks. "a\nb\nc".splitlines() → ["a", "b", "c"] |
sep.join(iterable) | Join strings with separator. ",".join(["a","b","c"]) → "a,b,c" |
s.partition(sep) | Split into 3-tuple at first sep. "a=b=c".partition("=") → ("a", "=", "b=c") |
s.rpartition(sep) | Split into 3-tuple at last sep. "a=b=c".rpartition("=") → ("a=b", "=", "c") |
s.removeprefix(prefix) | Remove prefix if present (Python 3.9+). "TestCase".removeprefix("Test") → "Case" |
s.removesuffix(suffix) | Remove suffix if present (Python 3.9+). "file.txt".removesuffix(".txt") → "file" |
| Method | Description |
s.isalpha() | True if all characters are letters. "hello".isalpha() → True |
s.isdigit() | True if all characters are digits. "123".isdigit() → True |
s.isnumeric() | True if all characters are numeric (includes fractions, superscripts). "½".isnumeric() → True |
s.isdecimal() | True if all characters are decimal digits (0-9). "42".isdecimal() → True |
s.isalnum() | True if all characters are alphanumeric. "abc123".isalnum() → True |
s.isspace() | True if all characters are whitespace. " \t\n".isspace() → True |
s.isupper() | True if all cased characters are uppercase. "HELLO".isupper() → True |
s.islower() | True if all cased characters are lowercase. "hello".islower() → True |
s.istitle() | True if string is titlecased. "Hello World".istitle() → True |
s.isidentifier() | True if string is a valid Python identifier. "my_var".isidentifier() → True |
s.isprintable() | True if all characters are printable. "hello\n".isprintable() → False |
s.isascii() | True if all characters are ASCII (Python 3.7+). "hello".isascii() → True |
| Method / Syntax | Description |
s.format(*args, **kwargs) | "Hello, {}!".format("world") → "Hello, world!" |
"{0} {1}".format(a, b) | Positional formatting. "{0} {1}".format("a", "b") → "a b" |
"{name}".format(name=val) | Keyword formatting. "{name}".format(name="Alice") → "Alice" |
f"Hello, {name}!" | f-string (Python 3.6+). Variables evaluated inline. f"2+2={2+2}" → "2+2=4" |
f"{value:.2f}" | f-string with format spec. f"{3.14159:.2f}" → "3.14" |
f"{value:>10}" | Right-align in 10-char field. f"{'hi':>10}" → " hi" |
f"{value:<10}" | Left-align in 10-char field. f"{'hi':<10}" → "hi " |
f"{value:^10}" | Center-align in 10-char field. f"{'hi':^10}" → " hi " |
f"{value:0>5}" | Pad with zeros. f"{'42':0>5}" → "00042" |
f"{n:,}" | Number with commas. f"{1000000:,}" → "1,000,000" |
f"{n:.2%}" | Format as percentage. f"{0.85:.2%}" → "85.00%" |
s.center(width) | Center string in field of given width. "hi".center(10) → " hi " |
s.center(width, fill) | Center with fill character. "hi".center(10, "-") → "----hi----" |
s.ljust(width) | Left-justify in field. "hi".ljust(10) → "hi " |
s.rjust(width) | Right-justify in field. "hi".rjust(10) → " hi" |
s.zfill(width) | Pad with zeros on the left. "42".zfill(5) → "00042" |
s.expandtabs(tabsize) | Replace tabs with spaces. "a\tb".expandtabs(4) → "a b" |
| Method | Description |
s.encode(encoding="utf-8") | Encode string to bytes. "hello".encode() → b"hello" |
s.encode("ascii", "ignore") | Encode to ASCII, ignoring errors for non-ASCII characters |
s.encode("ascii", "replace") | Encode to ASCII, replacing non-ASCII characters with ? |
b.decode(encoding="utf-8") | Decode bytes to string. b"hello".decode() → "hello" |
b.decode("latin-1") | Decode bytes using Latin-1 encoding |
s.encode("unicode_escape") | Escape Unicode characters. "\u00e9".encode("unicode_escape") → b"\\xe9" |
| Pattern / Function | Description |
re.search(pattern, s) | Search for first match anywhere in string. Returns match object or None |
re.match(pattern, s) | Match pattern at the beginning of string only |
re.fullmatch(pattern, s) | Match if entire string matches the pattern |
re.findall(pattern, s) | Return list of all non-overlapping matches. re.findall(r"\d+", "a1b2") → ["1", "2"] |
re.finditer(pattern, s) | Return iterator of match objects for all matches |
re.sub(pattern, repl, s) | Replace all matches. re.sub(r"\d", "X", "a1b2") → "aXbX" |
re.sub(pattern, repl, s, count=1) | Replace only the first match |
re.split(pattern, s) | Split by regex. re.split(r"[,;]", "a,b;c") → ["a", "b", "c"] |
re.compile(pattern) | Compile a pattern for reuse. pat = re.compile(r"\d+") |
match.group() | Return the matched string from a match object |
match.group(1) | Return the first capture group from the match |
match.groups() | Return a tuple of all capture groups |
re.IGNORECASE / re.I | Flag for case-insensitive matching |
re.MULTILINE / re.M | Flag: ^ and $ match start/end of each line |
re.DOTALL / re.S | Flag: . matches any character including newlines |