String Methods

Learn about the different string methods available in Python.

Introduction to string methods

Here are some of the string methods available in the string module (in alphabetical order):

  • string.center(int) returns a copy of string centered in a string of length int.

  • string.count(substring) returns the number of non-overlapping occurrences of substring in string.

  • string.endswith(suffix) returns True if string ends with suffix.

  • string.find(substring) returns the index at the beginning of the first occurrence of substring in string, or it returns -1 if not found.

  • string.isalnum() tests whether all characters are alphanumeric (letters or digits). It returns True if all characters are alphanumeric or False otherwise (including if the string is empty).

  • string.isalpha() tests whether all characters are alphabetic. It returns True if all characters are alphabetic or False otherwise (including if the string is empty).

  • string.isdigit() tests whether all characters are digits. It returns True if all characters are digits or False otherwise.

  • string.isidentifier() returns True if string is a nonempty string consisting only of letters, digits, and/or underscores, and does not begin with a digit. It returns False if the string is empty, begins with a digit, or contains any other characters.

  • string.islower() tests whether all letters in string are lowercase. It returns True if all characters are lowercase or False otherwise (including if the string is empty).

  • string.isprintable() tests whether the string does not contain control characters. It returns True if all characters can be printed, or False otherwise.

  • string.isspace() tests whether all characters are whitespace (spaces, tabs, newlines, and some Unicode characters). It returns True if all characters are whitespaces or False otherwise (including if the string is empty).

  • string.isupper() tests whether all letters in string are uppercase. It returns True if all characters are uppercase or False otherwise (including if the string is empty).

  • string.ljust(int) returns a copy of string left-justified in a field of length int.

  • string.lower() returns a copy of string with all uppercase letters replaced by their lowercase equivalents.

  • string1.partition(string2) returns a 3-tuple: (the part of string1 before string2, string2 itself, and the part after string2).

  • string1.replace(string2, string3) returns a copy of string1 with all occurrences of string2 replaced with string3.

  • string.rjust(int) returns a copy of the string right-justified in a field of length int.

  • string1.split(string2) returns a list of the substrings of string1 that are separated by string2. If string2 is omitted, whitespace is used as the separator.

  • string.splitlines() returns a list of the lines in string, discarding newlines.

  • string.startswith(prefix) returns True if string starts with prefix.

  • string.strip() returns a copy of string with all leading and trailing whitespace removed.

  • string.upper() returns a copy of string with all lowercase letters replaced by their uppercase equivalents.

  • string.join() returns a string by joining all the elements of a sequence/iterable separated by a string separator.

  • string.format() formats the specified value(s) and inserts them in string’s placeholder(s).

In addition: A string may be treated as a list of characters, so all of the list methods can be applied to strings.

Get hands-on with 1400+ tech skills courses.