■ Python String Methods Cheat Sheet (47 Total)
Method Description Example
capitalize() Capitalize first letter "hello".capitalize() → "Hello"
casefold() Aggressive lowercase "HELLOß".casefold() → "helloß"
center() Center string "hi".center(6,"-") → "--hi--"
count() Count substring "banana".count("a") → 3
encode() Encode to bytes "hi".encode() → b'hi'
endswith() Check ending "hello".endswith("lo") → True
expandtabs() Replace tabs with spaces "a\tb".expandtabs(4) → "a b"
find() Find substring index "hello".find("e") → 1
format() String formatting "{} + {}".format(2,3) → "2 + 3"
format_map() Format with dict "{x}".format_map({"x":10}) → "10"
index() Find index (error if not found) "hi".index("i") → 1
isalnum() Letters/digits only? "abc123".isalnum() → True
isalpha() Alphabet only? "abc".isalpha() → True
isascii() ASCII only? "abc".isascii() → True
isdecimal() Decimal only? "123".isdecimal() → True
isdigit() Digits only? "²3".isdigit() → True
isidentifier() Valid Python name? "var1".isidentifier() → True
islower() Lowercase only? "hello".islower() → True
isnumeric() Numeric only? "■".isnumeric() → True
isprintable() Printable only? "hi".isprintable() → True
isspace() Whitespace only? " ".isspace() → True
istitle() Title case? "Hello World".istitle() → True
isupper() Uppercase only? "HELLO".isupper() → True
join() Join iterable ",".join(["a","b"]) → "a,b"
ljust() Left justify "hi".ljust(5,"-") → "hi---"
lower() Lowercase "HI".lower() → "hi"
lstrip() Trim left spaces " hi".lstrip() → "hi"
maketrans() Mapping for translate() [Link]('ab','12')
partition() Split into 3 parts "a-b".partition("-") → ("a","-","b")
removeprefix() Remove prefix "unhappy".removeprefix("un") → "happy"
removesuffix() Remove suffix "flying".removesuffix("ing") → "fly"
replace() Replace substring "hi hi".replace("hi","hey") → "hey hey"
rfind() Find last occurrence "banana".rfind("a") → 5
rindex() Last index (error if not found) "banana".rindex("a") → 5
rjust() Right justify "hi".rjust(5,"-") → "---hi"
rpartition() Right split into 3 parts "a-b-c".rpartition("-") → ("a-b","-","c")
rsplit() Split from right "a,b,c".rsplit(",",1) → ["a,b","c"]
rstrip() Trim right spaces "hi ".rstrip() → "hi"
split() Split string "a b c".split() → ["a","b","c"]
splitlines() Split by lines "a\nb".splitlines() → ["a","b"]
startswith() Check start "hello".startswith("he") → True
strip() Trim both sides " hi ".strip() → "hi"
swapcase() Swap case "HeLLo".swapcase() → "hEllO"
title() Title case "hello world".title() → "Hello World"
translate() Replace using maketrans() "abc".translate([Link]("ab","12")) → "12c"
upper() Uppercase "hi".upper() → "HI"
zfill() Pad with zeros "7".zfill(3) → "007"