Python String Methods
Method Description Example
capitalize() Converts first character to uppercase "hello".capitalize() -> "Hello"
casefold() Converts to lowercase, more aggressive "HELLO".casefold()
than lower() -> "hello"
lower() Converts all characters to lowercase "Hello".lower() -> "hello"
upper() Converts all characters to uppercase "Hello".upper() -> "HELLO"
title() Converts to title case "hello world".title() -> "Hello World"
swapcase() Swaps case of all characters "HeLLo".swapcase() -> "hEllO"
strip() Removes leading/trailing whitespace " hello ".strip() -> "hello"
lstrip() Removes leading whitespace " hello".lstrip() -> "hello"
rstrip() Removes trailing whitespace "hello ".rstrip() -> "hello"
replace() Replaces substring with another "hello".replace("l", "x") -> "hexxo"
find() Finds index of first occurrence, -1 if not found
"hello".find("l") -> 2
rfind() Finds last occurrence "hello".rfind("l") -> 3
index() Like find(), but raises error if not found "hello".index("l") -> 2
rindex() Like rfind(), but raises error if not found "hello".rindex("l") -> 3
split() Splits string by delimiter "a,b,c".split(",") -> ['a', 'b', 'c']
rsplit() Splits from right "a,b,c".rsplit(",", 1) -> ['a,b', 'c']
join() Joins iterable with string ",".join(["a", "b"]) -> "a,b"
startswith() Checks if string starts with substring "hello".startswith("he") -> True
endswith() Checks if string ends with substring "hello".endswith("lo") -> True
isalpha() Returns True if all characters are alphabetic
"abc".isalpha() -> True
isdigit() Returns True if all characters are digits "123".isdigit() -> True
isalnum() Returns True if all characters are alphanumeric
"abc123".isalnum() -> True
isspace() Returns True if all characters are whitespace
" ".isspace() -> True
isupper() Returns True if all characters are uppercase
"HELLO".isupper() -> True
islower() Returns True if all characters are lowercase
"hello".islower() -> True
istitle() Returns True if string is in title case "Hello World".istitle() -> True
zfill() Pads string on the left with zeros "42".zfill(5) -> "00042"
center() Centers string in given width "hi".center(6) -> " hi "
ljust() Left-justifies string "hi".ljust(6) -> "hi "
rjust() Right-justifies string "hi".rjust(6) -> " hi"