DD
DevDash

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

CommandDescription
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

CommandDescription
s.strip()

Remove leading and trailing whitespace

" hello ".strip() # "hello"
s.lstrip()

Remove leading whitespace only

s.rstrip()

Remove trailing whitespace only

Replace

CommandDescription
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

CommandDescription
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

CommandDescription
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

CommandDescription
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

CommandDescription
s.isdigit()

Check if all characters are digits

"123".isdigit() # True

Frequently Asked Questions

More Cheatsheets

Want API access + no ads? Pro coming soon.