Python 3 reference · 47 entries

Python String Methods

Every Python string method, from capitalize() to zfill(), with syntax, parameters and runnable examples. Strings are immutable, so every method returns a new string.

Call a method on any string with text.method(). Strings never change in place: methods such as replace() and upper() return a new string, so assign the result (text = text.strip()).

NameDescriptionSince
str.capitalize() Return a copy of the string with the first character in upper case and the rest in lower case. 3.0
str.casefold() Return a case-folded copy for caseless comparison. Stronger than lower(): it also handles special cases like German ß. 3.3
str.center() Return the string centred in a field of the given width, padded on both sides with a fill character (a space by default). 3.0
str.count() Return the number of non-overlapping occurrences of a substring, optionally only between start and end positions. 3.0
str.encode() Encode the string into bytes using a text encoding such as UTF-8 (the default), for files, networks and hashing. 3.0
str.endswith() Return True if the string ends with the given suffix. Pass a tuple to check several suffixes at once. 3.0
str.expandtabs() Return a copy where tab characters are replaced by spaces, aligning to tab stops every tabsize columns (8 by default). 3.0
str.find() Return the lowest index where a substring is found, or -1 if it isn't. Optional start and end limit the search. 3.0
str.format() Return a formatted string, filling {} placeholders with the arguments. f-strings are usually simpler. 3.0
str.format_map() Like format(), but takes the values from a single mapping, which can be a dict or a dict subclass with defaults. 3.2
str.index() Return the lowest index of a substring, like find(), but raise ValueError when it is not found. 3.0
str.isalnum() Return True if every character is a letter or a digit and the string isn't empty. 3.0
str.isalpha() Return True if every character is a letter (in any alphabet) and the string isn't empty. Spaces and digits return False. 3.0
str.isascii() Return True if the string is empty or every character is ASCII (code points 0–127). 3.7
str.isdecimal() Return True if every character is a decimal digit (0–9 and their equivalents in other scripts). The strictest digit check. 3.0
str.isdigit() Return True if every character is a digit, including superscripts like ². Signs, spaces and decimal points return False. 3.0
str.isidentifier() Return True if the string is a valid Python identifier: a name you could use for a variable, function or class. 3.0
str.islower() Return True if all cased characters are lower case and there is at least one cased character. 3.0
str.isnumeric() Return True if every character is numeric, including digits, fractions like ½ and numerals from other scripts. 3.0
str.isprintable() Return True if every character is printable, or the string is empty. Newlines and tabs are not printable. 3.0
str.isspace() Return True if every character is whitespace (spaces, tabs, newlines) and the string isn't empty. 3.0
str.istitle() Return True if the string is title-cased: each word starts with an upper-case letter followed by lower-case letters. 3.0
str.isupper() Return True if all cased characters are upper case and there is at least one cased character. 3.0
str.join() Join the strings of an iterable into one string, with this string as the separator between them. 3.0
str.ljust() Return the string left-justified in a field of the given width, padded on the right with a fill character. 3.0
str.lower() Return a copy of the string with all cased characters converted to lower case. 3.0
str.lstrip() Return a copy with leading characters removed: whitespace by default, or any of the characters you pass. 3.0
str.maketrans() Create a translation table for translate(): map characters to replacements, or to None to delete them. 3.0
str.partition() Split the string at the first occurrence of a separator into a 3-tuple: the part before, the separator, and the part after. 3.0
str.removeprefix() Return the string without the given prefix if it starts with it; otherwise return it unchanged. 3.9
str.removesuffix() Return the string without the given suffix if it ends with it; otherwise return it unchanged. 3.9
str.replace() Return a copy with every occurrence of old replaced by new, or only the first count occurrences. 3.0
str.rfind() Return the highest index where a substring is found, searching from the right, or -1 if it isn't found. 3.0
str.rindex() Return the highest index of a substring, like rfind(), but raise ValueError when it is not found. 3.0
str.rjust() Return the string right-justified in a field of the given width, padded on the left with a fill character. 3.0
str.rpartition() Split the string at the last occurrence of a separator into a 3-tuple: before, separator and after. 3.0
str.rsplit() Split the string into a list from the right. With maxsplit, only the last few splits are made. 3.0
str.rstrip() Return a copy with trailing characters removed: whitespace by default, or any of the characters you pass. 3.0
str.split() Split the string into a list of substrings at a separator. With no separator it splits on any whitespace. 3.0
str.splitlines() Split the string into a list of lines at line boundaries (\n, \r\n and others), without the line endings. 3.0
str.startswith() Return True if the string starts with the given prefix. Pass a tuple to check several prefixes at once. 3.0
str.strip() Return a copy with leading and trailing characters removed: whitespace by default, or any of the characters you pass. 3.0
str.swapcase() Return a copy with upper-case letters converted to lower case and vice versa. 3.0
str.title() Return a title-cased copy: the first letter of each word upper case and the rest lower case. 3.0
str.translate() Return a copy where each character is mapped through a translation table, replacing or deleting characters fast. 3.0
str.upper() Return a copy of the string with all cased characters converted to upper case. 3.0
str.zfill() Return the string padded on the left with zeros to the given width, keeping a leading + or - sign in front. 3.0