Python String Methods - Quick Reference
strip()
Removes leading and trailing whitespaces or characters.
Example: ' hello '.strip() -> 'hello'
lstrip()
Removes leading whitespaces.
Example: ' hello '.lstrip() -> 'hello '
rstrip()
Removes trailing whitespaces.
Example: ' hello '.rstrip() -> ' hello'
lower()
Converts all characters to lowercase.
Example: 'HELLO'.lower() -> 'hello'
upper()
Converts all characters to uppercase.
Example: 'hello'.upper() -> 'HELLO'
capitalize()
Capitalizes the first letter.
Example: 'python'.capitalize() -> 'Python'
title()
Capitalizes the first letter of each word.
Example: 'hello world'.title() -> 'Hello World'
Python String Methods - Quick Reference
replace(old, new)
Replaces substrings.
Example: 'apple'.replace('a', 'o') -> 'opple'
split(separator)
Splits string into a list.
Example: 'a,b,c'.split(',') -> ['a', 'b', 'c']
join(iterable)
Joins elements with a separator.
Example: ','.join(['a', 'b']) -> 'a,b'
find(sub)
Returns the index of the first occurrence.
Example: 'hello'.find('e') -> 1
startswith(prefix)
Checks if string starts with prefix.
Example: 'hello'.startswith('he') -> True
endswith(suffix)
Checks if string ends with suffix.
Example: 'test.py'.endswith('.py') -> True
isalpha()
Checks if all characters are letters.
Example: 'abc'.isalpha() -> True
Python String Methods - Quick Reference
isdigit()
Checks if all characters are digits.
Example: '123'.isdigit() -> True
isalnum()
Checks if all characters are alphanumeric.
Example: 'abc123'.isalnum() -> True
isspace()
Checks if string contains only whitespace.
Example: ' '.isspace() -> True