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.

Learn more

casefold()

Returns a copy of the string in folded case, which is similar to lowercase but more aggressive.

Learn more

center(width[, fillchar])

Centers the string, padding it on the left and right to the specified length with the specified character.

Learn more

count(sub[, start[, end]])

Returns the number of non-overlapping occurrences of the substring in the string.

Learn more

encode(encoding="utf-8", errors="strict")

Encodes the string into bytes using the specified codec.

Learn more

endswith(suffix[, start[, end]])

Returns True or False indicating if the string ends with the specified suffix.

Learn more

expandtabs(tabsize=8)

Returns a copy of the string where tab characters are replaced with spaces.

Learn more

find(sub[, start[, end]])

Searches for a substring in the string. Returns the lowest index where the substring is found.

Learn more

format(args, *kwargs)

Returns a formatted copy of the string, replacing markers in curly braces {} with values from the arguments.

Learn more

index(sub[, start[, end]])

Returns the lowest index where the substring is found in the string.

Learn more

isalnum()

Returns True or False indicating if the string contains only letters and/or digits.

Learn more

isalpha()

Returns True or False indicating if the string contains only letters.

Learn more

isascii()

Returns True or False indicating if the string contains only ASCII characters.

Learn more

isdigit()

Returns True or False indicating if the string contains only digits.

Learn more

isidentifier()

Returns True or False indicating if the string is a valid identifier.

Learn more

islower()

Returns True or False indicating if the string contains only lowercase characters.

Learn more

isnumeric()

Returns True or False indicating if the string contains only numeric characters.

Learn more

isprintable()

Returns True or False indicating if all characters in the string are printable. Returns True if the string is empty.

Learn more

isspace()

Returns True or False indicating if the string contains only whitespace characters.

Learn more

istitle()

Returns True or False indicating if each “word” in the string begins with an uppercase letter.

Learn more

isupper()

Returns True or False indicating if the string contains only uppercase characters.

Learn more

join(iterable)

Returns a string constructed from the elements of the specified iterable.

Learn more

ljust(width[, fillchar])

Left-aligns the string, padding it on the right to the specified length with the specified character.

Learn more

lower()

Returns a copy of the string with all characters in lowercase.

Learn more

lstrip([chars])

Returns a copy of the string with the specified characters removed from the beginning.

Learn more

maketrans(x[, y[, z]])

Returns a translation table for use in the translate method.

Learn more

partition(sep)

Splits the string into three parts (start, separator, end) and returns them as a tuple.

Learn more

replace(old, new[, count])

Returns a copy of the string with all occurrences of the substring replaced with the specified value.

Learn more

rfind(sub[, start[, end]])

Returns the index of the last occurrence of the substring. Returns −1 if not found.

Learn more

rindex(sub[, start[, end]])

Returns the highest index where the substring is found in the string.

Learn more

rjust(width[, fillchar])

Right-aligns the string, padding it on the left to the specified length with the specified character.

Learn more

rpartition(sep)

Splits the string into three parts (start, separator, end) and returns them as a tuple, splitting from right to left.

Learn more

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.

Learn more

rstrip([chars])

Returns a copy of the string with the specified characters removed from the end.

Learn more

split(sep=None, maxsplit=-1)

Splits the string into parts using the separator and returns these parts as a list.

Learn more

splitlines([keepends])

Splits the string into multiple lines and returns them as a list.

Learn more

startswith(prefix[, start[, end]])

Returns True or False indicating if the string starts with the specified prefix.

Learn more

strip([chars])

Returns a copy of the string with the specified characters removed from both ends.

Learn more

swapcase()

Returns a copy of the string with each letter having the opposite case.

Learn more

title()

Returns a copy of the string where each new word starts with an uppercase letter and continues with lowercase.

Learn more

translate(table)

Returns a string transformed using the translation table from maketrans.

Learn more

upper()

Returns a copy of the string with all characters in uppercase.

Learn more

zfill(width)

Pads the string with zeros on the left to the specified minimum length.

Learn more