Python String Methods
Methods are functions bound to an object. Strings are immutable, so string methods do not change the original string but create a modified copy. Call a method using a dot after the string, and pass arguments to the method like functions.
Below is a list of all Python string methods, sorted alphabetically.
capitalize()
Returns a copy of the string with the first letter in uppercase and the rest in lowercase.
casefold()
Returns a copy of the string in folded case, which is similar to lowercase but more aggressive.
center(width[, fillchar])
Centers the string, padding it on the left and right to the specified length with the specified character.
count(sub[, start[, end]])
Returns the number of non-overlapping occurrences of the substring in the string.
encode(encoding="utf-8", errors="strict")
Encodes the string into bytes using the specified codec.
endswith(suffix[, start[, end]])
Returns True or False indicating if the string ends with the specified suffix.
expandtabs(tabsize=8)
Returns a copy of the string where tab characters are replaced with spaces.
find(sub[, start[, end]])
Searches for a substring in the string. Returns the lowest index where the substring is found.
format(args, *kwargs)
Returns a formatted copy of the string, replacing markers in curly braces {} with values from the arguments.
index(sub[, start[, end]])
Returns the lowest index where the substring is found in the string.
isalnum()
Returns True or False indicating if the string contains only letters and/or digits.
isalpha()
Returns True or False indicating if the string contains only letters.
isascii()
Returns True or False indicating if the string contains only ASCII characters.
isdigit()
Returns True or False indicating if the string contains only digits.
isidentifier()
Returns True or False indicating if the string is a valid identifier.
islower()
Returns True or False indicating if the string contains only lowercase characters.
isnumeric()
Returns True or False indicating if the string contains only numeric characters.
isprintable()
Returns True or False indicating if all characters in the string are printable. Returns True if the string is empty.
isspace()
Returns True or False indicating if the string contains only whitespace characters.
istitle()
Returns True or False indicating if each “word” in the string begins with an uppercase letter.
isupper()
Returns True or False indicating if the string contains only uppercase characters.
join(iterable)
Returns a string constructed from the elements of the specified iterable.
ljust(width[, fillchar])
Left-aligns the string, padding it on the right to the specified length with the specified character.
lower()
Returns a copy of the string with all characters in lowercase.
lstrip([chars])
Returns a copy of the string with the specified characters removed from the beginning.
maketrans(x[, y[, z]])
Returns a translation table for use in the translate method.
partition(sep)
Splits the string into three parts (start, separator, end) and returns them as a tuple.
replace(old, new[, count])
Returns a copy of the string with all occurrences of the substring replaced with the specified value.
rfind(sub[, start[, end]])
Returns the index of the last occurrence of the substring. Returns −1 if not found.
rindex(sub[, start[, end]])
Returns the highest index where the substring is found in the string.
rjust(width[, fillchar])
Right-aligns the string, padding it on the left to the specified length with the specified character.
rpartition(sep)
Splits the string into three parts (start, separator, end) and returns them as a tuple, splitting from right to left.
rsplit(sep=None, maxsplit=-1)
Splits the string into parts using the separator and returns these parts as a list, splitting from right to left.
rstrip([chars])
Returns a copy of the string with the specified characters removed from the end.
split(sep=None, maxsplit=-1)
Splits the string into parts using the separator and returns these parts as a list.
splitlines([keepends])
Splits the string into multiple lines and returns them as a list.
startswith(prefix[, start[, end]])
Returns True or False indicating if the string starts with the specified prefix.
strip([chars])
Returns a copy of the string with the specified characters removed from both ends.
swapcase()
Returns a copy of the string with each letter having the opposite case.
title()
Returns a copy of the string where each new word starts with an uppercase letter and continues with lowercase.
translate(table)
Returns a string transformed using the translation table from maketrans.
upper()
Returns a copy of the string with all characters in uppercase.
zfill(width)
Pads the string with zeros on the left to the specified minimum length.