Last updated: April 12, 2026
Python String Methods Cheatsheet
Quick Answer
Python strings are immutable — all methods return new strings. Use split() to tokenize, join() to combine, strip() to trim, replace() for substitution, and f-strings for formatting. This covers the 20 most useful methods.
Split/Join
| Command | Description |
|---|---|
| s.split(sep) | Split string into a list by separator "a,b,c".split(",") # ["a", "b", "c"] |
| s.split() | Split by whitespace and remove empty strings " hello world ".split() # ["hello", "world"] |
| sep.join(iterable) | Join list elements with separator ", ".join(["a", "b", "c"]) # "a, b, c" |
Trim
| Command | Description |
|---|---|
| s.strip() | Remove leading and trailing whitespace " hello ".strip() # "hello" |
| s.lstrip() | Remove leading whitespace only |
| s.rstrip() | Remove trailing whitespace only |
Replace
| Command | Description |
|---|---|
| s.replace(old, new) | Replace all occurrences of a substring "hello world".replace("world", "Python") # "hello Python" |
| s.replace(old, new, count) | Replace only the first count occurrences "aaa".replace("a", "b", 2) # "bba" |
Search
| Command | Description |
|---|---|
| s.find(sub) | Return index of first occurrence (-1 if not found) "hello".find("ll") # 2 |
| s.index(sub) | Like find() but raises ValueError if not found |
| s.startswith(prefix) | Check if string starts with prefix "hello".startswith("he") # True |
| s.endswith(suffix) | Check if string ends with suffix "file.py".endswith(".py") # True |
| s.count(sub) | Count non-overlapping occurrences "banana".count("a") # 3 |
Case
| Command | Description |
|---|---|
| s.upper() | Convert to uppercase "hello".upper() # "HELLO" |
| s.lower() | Convert to lowercase "HELLO".lower() # "hello" |
| s.title() | Capitalize first letter of each word "hello world".title() # "Hello World" |
| s.capitalize() | Capitalize only the first character "hello world".capitalize() # "Hello world" |
Format
| Command | Description |
|---|---|
| f"...{expr}..." | F-string formatting (Python 3.6+) f"Hello, {name}!" # "Hello, Alice!" |
| s.zfill(width) | Pad with zeros on the left to fill width "42".zfill(5) # "00042" |
Validation
| Command | Description |
|---|---|
| s.isdigit() | Check if all characters are digits "123".isdigit() # True |
Frequently Asked Questions
More Cheatsheets
Git Commands Cheatsheet
This cheatsheet covers the 40 most essential Git commands grouped by workflow: setup, staging, branc...
Docker Commands Cheatsheet
This cheatsheet covers 35 essential Docker commands for managing containers, images, volumes, and ne...
Linux Commands Cheatsheet
This cheatsheet covers 40 essential Linux/Unix commands for daily development: file navigation, text...
HTTP Status Codes Cheatsheet
HTTP status codes are 3-digit numbers returned by servers. 1xx = informational, 2xx = success, 3xx =...