Python string methods
Clean, split, search and reformat text: split, join, strip, replace, find, upper and more.
String methods work on text. You call them with a dot after a string (name.upper()). Strings never change in place, so every method gives you back a new string (or a list, a number or True/False) and leaves the original as it was.
Most used
str.count()Counts how many times a piece of text appears in a string, without overlaps, in the whole string or in a part of it.str.find()Returns the position where a piece of text first appears in a string, or -1 when it is not there at all.str.format()Fills values into the {} placeholders of a string, with control over decimals, width, alignment and thousand separators.str.isdigit()Returns True when a string is not empty and every character is a digit. Useful for checking input before int().str.join()Glues the strings in a list (or any iterable) into one string, with the text you call it on placed between every piece.str.lower()Returns a copy of a string with every capital letter turned into a small (lower-case) letter. Useful for comparing text.str.replace()Returns a copy of a string where every match of one piece of text is swapped for another, or only the first few matches.str.split()Cuts a string into a list of pieces: into words by default, or at any separator you choose, such as a comma.str.startswith()Returns True when a string begins with the text you give, or with any one of several options passed as a tuple.str.strip()Removes spaces, tabs and new lines from the start and end of a string, or any set of characters you list.str.upper()Returns a copy of a string with every letter turned into a capital (upper-case) letter. Digits and symbols stay the same.
All 33 string methods
- str.capitalize()Returns a copy of a string with its first character in capitals and all the other letters turned into small letters.
- str.casefold()Returns a version of a string made for case-insensitive comparison. Stronger than lower() for text in any language.
- str.center()Returns a copy of a string centred in a field of the width you choose, padded with spaces or another single character.
- str.count()Counts how many times a piece of text appears in a string, without overlaps, in the whole string or in a part of it.
- str.encode()Converts a string into bytes using a text encoding, UTF-8 by default, ready for files, networks or hashing.
- str.endswith()Returns True when a string ends with the text you give, or with any one of several endings passed as a tuple.
- str.find()Returns the position where a piece of text first appears in a string, or -1 when it is not there at all.
- str.format()Fills values into the {} placeholders of a string, with control over decimals, width, alignment and thousand separators.
- str.index()Returns the position where a piece of text first appears in a string, and raises ValueError when it is not found.
- str.isalnum()Returns True when a string is not empty and every character is a letter or a number. Spaces and symbols make it False.
- str.isalpha()Returns True when a string is not empty and every character is a letter, in any alphabet. Spaces and digits make it False.
- str.isdigit()Returns True when a string is not empty and every character is a digit. Useful for checking input before int().
- str.isnumeric()Returns True when every character is a number of some kind: digits, superscripts, fractions like ½ or Roman numerals.
- str.isspace()Returns True when a string is not empty and holds only whitespace: spaces, tabs, new lines and similar blank characters.
- str.join()Glues the strings in a list (or any iterable) into one string, with the text you call it on placed between every piece.
- str.ljust()Returns a copy of a string padded on the right to the width you choose, so the text lines up on the left side.
- str.lower()Returns a copy of a string with every capital letter turned into a small (lower-case) letter. Useful for comparing text.
- str.lstrip()Removes whitespace, or any characters you list, from the start (left side) of a string. The right side stays as it is.
- str.partition()Splits a string at the first match of a separator and returns a tuple of three: the part before, the separator, the part after.
- str.removeprefix()Removes an exact piece of text from the start of a string, if the string begins with it. Otherwise the string comes back as it is.
- str.removesuffix()Removes an exact piece of text from the end of a string, if the string ends with it. Otherwise the string comes back as it is.
- str.replace()Returns a copy of a string where every match of one piece of text is swapped for another, or only the first few matches.
- str.rjust()Returns a copy of a string padded on the left to the width you choose, so the text lines up on the right side.
- str.rsplit()Cuts a string into a list of pieces like split(), but when you limit the cuts with maxsplit it starts from the right end.
- str.rstrip()Removes whitespace, or any characters you list, from the end (right side) of a string, such as the new line at the end of a line.
- str.split()Cuts a string into a list of pieces: into words by default, or at any separator you choose, such as a comma.
- str.splitlines()Splits a text into a list of lines at every line break, whether it is written as \n, \r\n or \r, without an empty last line.
- str.startswith()Returns True when a string begins with the text you give, or with any one of several options passed as a tuple.
- str.strip()Removes spaces, tabs and new lines from the start and end of a string, or any set of characters you list.
- str.swapcase()Returns a copy of a string where capital letters become small and small letters become capitals. Other characters stay.
- str.title()Returns a copy of a string with the first letter of every word in capitals and the other letters small, like a book title.
- str.upper()Returns a copy of a string with every letter turned into a capital (upper-case) letter. Digits and symbols stay the same.
- str.zfill()Adds zeros to the left of a string until it reaches the width you ask for, keeping a + or - sign at the front.