Reference to Python String Methods
In Python, string methods are built-in functions tied to string objects that help manipulate and work with text. These methods are readily available in the Python programming language for efficient string handling. This guide provides a complete and updated list of all 40+ Python string methods for easy reference.
Alphabetical Reference
The following table lists all 47 string related methods in Python in alphabetical order.
| Method | Description |
|---|---|
| capitalize() | This string method in Python converts the first letter of a string to uppercase. |
| casefold() | To convert a string to lowercase, we can use this Python method. |
| center() | To center a string in a field of a specified width, we use this Python method. |
| count() | This Python method counts the number of occurrences of a substring in a string. |
| encode() | In order to get an encoded string in bytes, we utilize this Python method. |
| endswith() | We use this method to check if the string ends with the provided suffix and returns True. |
| expandtabs() | Sets the tab size of the string to the required string using this Python method. |
| find() | This Python method is used to find the first occurrence of a substring in a string and returns its index or return -1. |
| format() | To format a string using template-style syntax, we use this Python method. |
| format_map() | This Python method formats a string using a mapping object. |
| index() | This Python method is similar to the find() function but raises an exception if the substring is not found in a string. |
| isalnum() | If all characters in the string are alphanumeric, this Python method returns True. |
| isalpha() | If all characters in the string are in the alphabet, this Python method returns True. |
| isascii() | This Python method returns True if all characters in the string are ASCII characters. |
| isdecimal() | If all characters in the string are decimals, this Python method returns True. |
| isdigit() | This Python method returns True if all characters in the string are digits. |
| isidentifier() | If the string is an identifier, this Python method returns True. |
| islower() | If all characters in the string are lowercase, the Python islower() method returns True. |
| isnumeric() | This Python method returns True if all characters in the string are numeric. |
| isprintable() | This Python method returns True if all characters in the string are printable. |
| isspace() | If all characters in the string are whitespaces, this Python method returns True. |
| istitle() | If the string follows the rules of a title, this Python method returns True. |
| isupper() | This Python method returns True if all characters in the string are upper case. |
| join() | This Python method converts the elements of an iterable into a string. |
| ljust() | To return a left-justified version of the string, we use this Python method. |
| lower() | To convert a string into lowercase, we use this Python method. |
| lstrip() | The leading specified characters are removed using this Python method. |
| maketrans() | To Return a translation table for strings' translations, we use this Python method. |
| partition() | This Python method returns a 3-tuple containing the separator and the part before and after the separator. |
| removeprefix() | This Python method removes the prefix from a string and returns the remaining string. |
| removesuffix() | To remove the suffix from a string, we use this Python method. |
| replace() | To replace a substring with a specified value in a string, we use this Python method. |
| rfind() | It returns -1 if the substring is not found unlike the rindex() Python method, otherwise, it returns the index of the last occurrence of the substring in a string. |
| rindex() | To find the index of the last occurrence of a substring in a string, we use this Python method and it works similarly to the rfind() function but gives an error if the substring is not found. |
| rjust() | To return a right justified version of the string, we use this Python method. |
| rpartition() | This Python method returns a 3-tuple containing the separator, the part before the separator, and the part after it in the string. |
| rsplit() | This Python method returns a list of specified words in a string using a separator. |
| rstrip() | The trailing characters are removed in a string using this Python method. |
| split() | To split the string at the specified separator, and return a list, we use this Python method. |
| splitlines() | To return a list of strings at line breaks, we use this Python method. |
| startswith() | If the string starts with the specified prefix, this Python method returns True. |
| strip() | This Python method removes beginning and trailing characters from a string as specified in the method argument. |
| swapcase() | The lowercase letter becomes uppercase letters and vice versa using this Python method. |
| title() | To convert the first character/letter of each word of the string to uppercase, we use this Python method. |
| translate() | To translate a string using a given translation table, we use this Python method. |
| upper() | This Python method converts a string into upper case. |
| zfill() | This Python method pads a numeric string with zeros(0) on the left to fill a specified width. |
Dynamically Get Python String Methods
There are various methods in Python that deal specifically with the strings. To get an up-to-date and ultimate reference list of all the string related methods, use the below script.
Get Python String Methods
# Get the list of string methods
string_methods = [
method
for method in dir(str)
if callable(getattr(str, method))
and not method.startswith('__')
]
# Print the list of string methods
print(string_methods)
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Note:
It’s important to remember that these string methods are distinct from the built-in Python functions, so keep this difference in mind.