Table of Contents
TogglePython’s built-in str class provides a variety of methods to manipulate strings. Since strings are immutable, these methods return a new string with the desired modifications. This guide categorizes and explains the most commonly used string methods in Python.
These methods are used to change the case of characters in a string.
| Sr.No. | Method | Description |
|---|---|---|
| 1 | capitalize() |
Capitalizes the first letter of the string. |
| 2 | casefold() |
Converts all uppercase letters to lowercase (works with Unicode characters). |
| 3 | lower() |
Converts all uppercase letters to lowercase. |
| 4 | swapcase() |
Inverts the case of all letters in the string. |
| 5 | title() |
Converts the string to title case (each word starts with an uppercase letter). |
| 6 | upper() |
Converts all lowercase letters to uppercase. |
These methods control the alignment of characters within a string.
| Sr.No. | Method | Description |
|---|---|---|
| 1 | center(width, fillchar) |
Returns a string centered in a specified width, padded with fillchar. |
| 2 | ljust(width[, fillchar]) |
Returns a string left-justified in a specified width, padded with fillchar. |
| 3 | rjust(width[, fillchar]) |
Returns a string right-justified in a specified width, padded with fillchar. |
| 4 | expandtabs(tabsize=8) |
Expands tabs in the string to multiple spaces (default is 8 spaces per tab). |
| 5 | zfill(width) |
Returns the string left-padded with zeros to a specified width. |
These methods are used to split strings into substrings or join multiple strings together.
| Sr.No. | Method | Description |
|---|---|---|
| 1 | lstrip() |
Removes all leading whitespace from the string. |
| 2 | rstrip() |
Removes all trailing whitespace from the string. |
| 3 | strip() |
Removes both leading and trailing whitespace from the string. |
| 4 | rsplit() |
Splits the string from the end and returns a list of substrings. |
| 5 | split() |
Splits the string according to a delimiter (default is space) and returns a list of substrings. |
| 6 | splitlines() |
Splits the string at line breaks and returns a list of lines. |
| 7 | partition() |
Splits the string into a tuple of three parts at the first occurrence of a separator. |
| 8 | rpartition() |
Splits the string into a tuple of three parts at the last occurrence of a separator. |
| 9 | join() |
Concatenates elements of a sequence into a single string, separated by the string. |
| 10 | removeprefix() |
Returns a string after removing the specified prefix. |
| 11 | removesuffix() |
Returns a string after removing the specified suffix. |
These methods return True or False based on specific conditions.
| Sr.No. | Method | Description |
|---|---|---|
| 1 | isalnum() |
Returns True if all characters in the string are alphanumeric. |
| 2 | isalpha() |
Returns True if all characters in the string are alphabetic. |
| 3 | isdigit() |
Returns True if all characters in the string are digits. |
| 4 | islower() |
Returns True if all cased characters in the string are lowercase. |
| 5 | isnumeric() |
Returns True if all characters in the string are numeric. |
| 6 | isspace() |
Returns True if all characters in the string are whitespace. |
| 7 | istitle() |
Returns True if the string is in title case. |
| 8 | isupper() |
Returns True if all cased characters in the string are uppercase. |
| 9 | isascii() |
Returns True if all characters in the string are ASCII. |
| 10 | isdecimal() |
Returns True if all characters in the string are decimal. |
| 11 | isidentifier() |
Returns True if the string is a valid Python identifier. |
| 12 | isprintable() |
Returns True if all characters in the string are printable. |
These methods are used to search for substrings and replace them.
| Sr.No. | Method | Description |
|---|---|---|
| 1 | count(sub, beg, end) |
Counts how many times a substring occurs in the string. |
| 2 | find(sub, beg, end) |
Returns the index of the first occurrence of a substring (or -1 if not found). |
| 3 | index(sub, beg, end) |
Same as find(), but raises an exception if the substring is not found. |
| 4 | replace(old, new[, max]) |
Replaces all occurrences of old with new (up to max replacements). |
| 5 | rfind(sub, beg, end) |
Same as find(), but searches backwards. |
| 6 | rindex(sub, beg, end) |
Same as index(), but searches backwards. |
| 7 | startswith(sub, beg, end) |
Returns True if the string starts with the specified substring. |
| 8 | endswith(suffix, beg, end) |
Returns True if the string ends with the specified suffix. |
These methods are used for translating characters in a string.
| Sr.No. | Method | Description |
|---|---|---|
| 1 | maketrans() |
Returns a translation table for use with translate(). |
| 2 | translate(table, deletechars="") |
Translates characters in the string using a translation table.str(256 chars), removing those in the del string. |
Enroll in our Python certification course at Vista Academy and become a certified expert to boost your career.
Enroll Now