← Reference

String Methods

Methods available on Python str objects.

str.capitalize()

The string capitalize method returns a copy of the string with its first character capitalized and the rest converted to lowercase

str.capitalize()

str.casefold()

The casefold method returns a casefolded copy of a string, useful for case-insensitive string matching and Unicode-aware lowercase conversion

str.casefold()

str.center()

The string center method returns a centered string of the specified width, padded with a specified fill character

str.center(width[, fillchar])

str.count()

The string count() method returns the number of non-overlapping occurrences of a substring in the string

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

str.encode()

The string encode method returns an encoded version of the string as a bytes object, with support for various character encodings

str.encode(encoding='utf-8', errors='strict')

str.endswith()

The string endswith method returns True if the string ends with the specified suffix, otherwise False

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

str.expandtabs()

The expandtabs method returns a copy of the string with tab characters expanded to spaces

str.expandtabs(tabsize=8)

str.find()

Find the index of a substring in a string. Returns -1 if not found.

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

str.format_map()

The string format_map method performs string formatting using a mapping object directly without copying it

str.format_map(mapping)

str.format()

Format values into strings using replacement fields and format specifiers

str.isalnum()

The isalnum method returns True if all characters in the string are alphanumeric (letters or digits) and the string is not empty

str.isalnum()

str.isalpha()

The isalpha method returns True if all characters in the string are alphabetic letters and the string is not empty

str.isalpha()

str.isascii()

The string isascii method returns True if all characters in the string are ASCII, False otherwise

str.isascii()

str.isdecimal()

Check if all characters in a string are decimal characters (under 160 chars)

str.isdecimal()

str.isdigit()

Check if all characters in a string are digits.

str.isidentifier()

Check if a string is a valid Python identifier

str.islower()

The islower method returns True if all cased characters in a string are lowercase and the string contains at least one cased character

str.islower()

str.isnumeric()

Check if all characters in a string are numeric characters (supports Unicode)

str.isnumeric()

str.isprintable()

The isprintable method returns True if all characters in the string are printable

str.isprintable()

str.isspace()

Returns True if all characters in the string are whitespace characters and the string is not empty.

str.istitle()

The istitle method returns True if the string is titlecased where each word starts with an uppercase character followed by lowercase letters

str.istitle()

str.isupper()

Returns True if all cased characters in a string are uppercase and there is at least one cased character.

str.isupper()

str.join()

The string join method returns a string by joining iterable elements with a separator

str.join(iterable)

str.ljust()

The string ljust method returns a left-justified version of the string padded with spaces or a custom character

str.ljust(width[, fillchar])

str.lower()

The lower method returns a copy of the string with all characters converted to lowercase

str.lower()

str.lstrip()

Remove leading characters from a string. Defaults to whitespace.

str.maketrans()

The str.maketrans() method creates a translation table for use with the translate() method

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

str.partition()

Split a string into three parts using a separator, returning a 3-tuple.

str.partition(sep)

str.removeprefix()

Remove a prefix from a string if present. Returns the original string unchanged if the prefix doesn't match.

str.removesuffix()

Remove a suffix from a string if present. Returns the original string unchanged if the suffix doesn't match.

str.replace()

Replace substrings in a string with a new value. Supports optional count limit.

str.rfind()

Find the highest index of a substring in a string. Returns -1 if not found.

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

str.rjust()

Right-justify a string in a field of given width.

str.rjust(width[, fillchar])

str.rpartition()

Split a string from the right into three parts using a separator, returning a 3-tuple.

str.rpartition(sep)

str.rsplit()

Split a string from the right into a list of substrings with rsplit().

str.rstrip()

Remove trailing characters from a string. Defaults to whitespace.

str.split()

Split a string into a list of substrings with the split() method.

str.splitlines()

Split a string into a list of lines, handling various line ending styles.

str.startswith()

Check if a string starts with a given prefix

str.strip()

Remove leading and trailing characters from a string. Defaults to whitespace.

str.swapcase()

Returns a string with uppercase characters converted to lowercase and vice versa.

str.swapcase()

str.title()

Returns a titlecased version of the string where words start with uppercase characters.

str.translate()

The translate method returns a copy of the string where each character has been mapped through a translation table

str.translate(table)

str.upper()

Converts all characters in a string to uppercase, returning a new string.

str.upper()

str.zfill()

Pad a string with zeros on the left to reach a desired width.