0% found this document useful (0 votes)
13 views1 page

Python String Methods CheatSheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Python String Methods CheatSheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

■ Python String Methods Cheat Sheet

■ Basic Methods
Method Description Example

str.lower() Makes all letters lowercase "HELLO".lower() → 'hello'


str.upper() Makes all letters uppercase "hello".upper() → 'HELLO'
str.capitalize() Capitalizes first letter only "python".capitalize() → 'Python'
str.title() Capitalizes first letter of each word "hello world".title() → 'Hello World'
str.strip() Removes spaces at beginning & end " hi ".strip() → 'hi'

■ Searching / Checking
Method Description Example

str.startswith(x) Checks if string starts with x "hello".startswith("he") → True


str.endswith(x) Checks if string ends with x "hello".endswith("lo") → True
str.find(x) Finds position of x (-1 if not found) "hello".find("l") → 2
str.count(x) Counts how many times x appears "hello".count("l") → 2
str.isdigit() Checks if all characters are numbers "123".isdigit() → True
str.isalpha() Checks if all are letters "abc".isalpha() → True

■ Modifying Strings
Method Description Example

str.replace(a, b) Replaces a with b "hello".replace("l", "z") → 'hezzo'


str.split(x) Splits string by x into a list "a,b,c".split(",") → ['a', 'b', 'c']
str.join(list) Joins list into one string ".".join(['a', 'b', 'c']) → 'a.b.c'
str.zfill(n) Pads with zeros on the left "7".zfill(3) → '007'
str.center(n) Centers string in n spaces "hi".center(5) → ' hi '

Example:
s = " hello world " print(s.strip().upper().replace("WORLD", "Python")) # Output: HELLO PYTHON

You might also like