Python: String Methods

By Xah Lee. Date: . Last updated: .

Python strings are not mutable. All string methods return a new string.

Substring

str[n:m]
Return a substring from index n to m.

Negative index counts from end, starting with -1

print("01234"[2:4] == "23")
print("abcd"[0:-2] == "ab")
print("abcd"[2:] == "cd")
str[:m]
From beginning to m.
str[n:]
From n to the end, including the end char.

Length

len(str)
Return the count of chars in str.

Join String

str1 + str2
Joins two strings into one.

Repeat

str * n
Repeat the string n times.

Search Substring

str.startswith(prefix, ?start, ?end)
Return True if string starts with the prefix, else False.
  • prefix can be a Tuple , each element is a string.
  • Optional ?start, ?end limits the range of positions to check.
str.endswith(suffix, ?start, ?end)
Similar to startwith, but check the end.
str.find(substr, ?start, ?end)
Return index of the first occurrence of substr. Return -1 if substr is not found.
str.rfind(substr, ?start, ?end)
Similar to find, but start at right.
str.index(substr, ?start, ?end)
Similar to find, but raise ValueError when the substring is not found.
str.rindex(substr, ?start, ?end)
Similar to index but start from right.
str.count(substr, ?start, ?end)
Return the count of non-overlapping occurrences of substring.

Find Replace

str.replace(old, new , ?count)
Return a new string, with occurrences of old replace by new. Only first ?count number of time is done.

Trim String

str.strip(?chars)
Remove any char in ?chars at the leading/trailing ends of the string.

The ?chars is a string specifying the set of characters to be removed, defaults to whitespace.

str.rstrip(?chars)
Same as strip, but only do trailing end.
str.lstrip(?chars)
Same as strip, but only do beginning end.

Convert List to String

str.join(iterable)
Change a List or Tuple into a string, by concatenating elements, and use str as separator.

Convert String to List

str.split(?sep, ?maxSplit)
Split string using sep as the delimiter. Return a list.

If sep is None, split by consecutive whitespaces, with leading or trailing whitespace removed.

print(" a   b  c ".split() == ["a", "b", "c"])

empty string return empty list.

print("".split() == [])

maxSplit is max count of split. (result length is at most maxSplit+1).

print("a b c d".split(" ", 2) == ["a", "b", "c d"])

If sep is given, it is taken as literal, not regex.

print("1,,2".split(",") == ["1", "", "2"])

sep can be multiple chars.

print("1,,2".split(",,") == ["1", "2"])

Splitting an empty string with a specified separator returns [''].

print("".split(",") == [""])
str.rsplit(?sep, ?maxsplit)
Same as split but begin at right.
str.splitlines(?keepends)
Return a list of the lines in the string, breaking at line boundaries.

This method uses the universal newlines approach to splitting lines.

Line breaks are not included in the resulting list unless keepends is given and true.

xx = "ab c\n\nde fg\rkl\r\n"

print(xx.splitlines() == ["ab c", "", "de fg", "kl"])

print(xx.splitlines(True) == ["ab c\n", "\n", "de fg\r", "kl\r\n"])

Empty string returns empty list

print("".splitlines() == [])
str.partition(sep)
Split the string at the first occurrence of sep, and return a 3-Tuple containing the part before the separator, the separator itself, and the part after the separator.

If the separator is not found, return (str, "", "")

str.rpartition(sep)
Same as partition but begin at right.

Check Character Case, Character Class

str.isalnum()
Return True if all characters are alphanumeric and there is at least one character, else False.
str.isalpha()
str.isdigit()
str.isupper()
str.islower()
str.isspace()
Check whitespace characters.
str.istitle()
True if every word start with cap letter.
unicode.isnumeric()
Return True if there are only numeric characters in S, False otherwise.

Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH.

unicode.isdecimal()
Return True if there are only decimal characters in unicode, False otherwise.

Decimal characters include digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.

str.translate(table, ?deleteChars)
Return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256.

You can use the maketrans() helper function in the string module to create a translation table. For string objects, set the table argument to None for translations that only delete characters:

Letter Case Conversion

str.capitalize()
Capitalize the first character
str.upper()
Change to uppercase.
str.lower()
Change to lowercase.
str.swapcase()
Switch uppercase/lowercase.
str.title()
Make each word's first letter uppercase.

Formatting Related Methods

str.format(args)
Formatting the string. (replace parts with arguments) [see Python: Format String].
str.center(n)
Add space to begin and end of string, so it's centered with respect to n chars.
str.center(n,char)
Fill it with character char
str.ljust(width, ?fillchar)
Add fillchar to the end of string, so total length is width.

fillchar defaults to space. The original string is returned if width is less than or equal to given string length.

print("abc".rjust(5) == "  abc")
print("abc".rjust(5, "-") == "--abc")
str.rjust(width, fillchar)
Same as ljust but done pads on the left.
str.zfill(width)
Return the numeric string left filled with zeros in a string of length width. A sign prefix is handled correctly. The original string is returned if width is less than or equal length.
str.expandtabs()
Replace tab char by space.
str.expandtabs(tabsize)

String Character Encoding/Decoding, Unicode

str.decode(coding)
Decode the string using coding.
str.encode(coding)
Encoded the string using coding.

For a list of possible encodings, see python doc “Standard Encodings”.

Python String