Python string methods are built-in functions that allow you to manipulate and analyze strings. Since strings in Python are immutable, these methods return a new string rather than modifying the original.
Core String Methods
Case Manipulation:
upper(): Converts all characters to uppercase.lower(): Converts all characters to lowercase.capitalize(): Capitalizes the first character and makes the rest lowercase.title(): Capitalizes the first letter of each word.swapcase(): Swaps the case of all characters.casefold(): More aggressive thanlower(); used for caseless matching.
Search and Find:
find(): Returns the index of the first occurrence of a substring (returns-1if not found).index(): Similar tofind(), but raises aValueErrorif not found.rfind(): Returns the highest index of a substring.rindex(): Similar torfind(), but raises an exception if not found.count(): Returns the number of times a substring appears.
Checking Conditions (Boolean):
isalnum(): ReturnsTrueif all characters are alphanumeric.isalpha(): ReturnsTrueif all characters are alphabetic.isdigit(): ReturnsTrueif all characters are digits.isnumeric(): ReturnsTrueif all characters are numeric (includes Unicode numerals).isidentifier(): Checks if the string is a valid Python identifier.islower(),isupper(): Check if all characters are lowercase or uppercase.isspace(): ReturnsTrueif the string contains only whitespace.istitle(): ReturnsTrueif the string is title-cased.isprintable(): ReturnsTrueif all characters are printable.
Trim and Align:
strip(): Removes leading and trailing whitespace (or specified characters).lstrip(): Removes leading whitespace.rstrip(): Removes trailing whitespace.center(width, fillchar): Centers the string within a specified width using a fill character.ljust(width, fillchar): Left-aligns the string.rjust(width, fillchar): Right-aligns the string.expandtabs(tabsize): Replaces tab characters with spaces.
Split and Join:
split(sep): Splits the string into a list using a separator (default is whitespace).rsplit(sep): Splits from the right.splitlines(): Splits the string at line boundaries.join(iterable): Joins elements of an iterable into a single string with the string as a separator.
Replace and Modify:
replace(old, new, count): Replaces occurrences of a substring with another.partition(sep): Splits the string into three parts at the first occurrence of a separator.rpartition(sep): Splits at the last occurrence of a separator.removeprefix(prefix): Removes a prefix if present (Python 3.9+).removesuffix(suffix): Removes a suffix if present (Python 3.9+).
Encoding and Translation:
encode(encoding): Encodes the string using the specified encoding.maketrans(x, y, z): Creates a translation table.translate(table): Applies a translation table to the string.
Starts/Ends With:
startswith(prefix): ReturnsTrueif the string starts with the given prefix.endswith(suffix): ReturnsTrueif the string ends with the given suffix.
Padding:
zfill(width): Pads the string with zeros on the left to fill the specified width.
For full details and examples, refer to the official Python documentation: Built-in Types — Python 3.14.3 documentation.