String Methods
Learn about the different string methods available in Python.
We'll cover the following
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 ofstring
centered in a string of lengthint
. -
string.count(substring)
returns the number of non-overlapping occurrences ofsubstring
instring
. -
string.endswith(suffix)
returnsTrue
ifstring
ends withsuffix
. -
string.find(substring)
returns the index at the beginning of the first occurrence ofsubstring
instring
, or it returns -1 if not found. -
string.isalnum()
tests whether all characters are alphanumeric (letters or digits). It returnsTrue
if all characters are alphanumeric orFalse
otherwise (including if thestring
is empty). -
string.isalpha()
tests whether all characters are alphabetic. It returnsTrue
if all characters are alphabetic orFalse
otherwise (including if thestring
is empty). -
string.isdigit()
tests whether all characters are digits. It returnsTrue
if all characters are digits orFalse
otherwise. -
string.isidentifier()
returnsTrue
ifstring
is a nonempty string consisting only of letters, digits, and/or underscores, and does not begin with a digit. It returnsFalse
if the string is empty, begins with a digit, or contains any other characters. -
string.islower()
tests whether all letters instring
are lowercase. It returnsTrue
if all characters are lowercase orFalse
otherwise (including if thestring
is empty). -
string.isprintable()
tests whether thestring
does not contain control characters. It returnsTrue
if all characters can be printed, orFalse
otherwise. -
string.isspace()
tests whether all characters are whitespace (spaces, tabs, newlines, and some Unicode characters). It returnsTrue
if all characters are whitespaces orFalse
otherwise (including if thestring
is empty). -
string.isupper()
tests whether all letters instring
are uppercase. It returnsTrue
if all characters are uppercase orFalse
otherwise (including if thestring
is empty). -
string.ljust(int)
returns a copy ofstring
left-justified in a field of lengthint
. -
string.lower()
returns a copy ofstring
with all uppercase letters replaced by their lowercase equivalents. -
string1.partition(string2)
returns a 3-tuple: (the part ofstring1
beforestring2
,string2
itself, and the part afterstring2
). -
string1.replace(string2, string3)
returns a copy ofstring1
with all occurrences ofstring2
replaced withstring3
. -
string.rjust(int)
returns a copy of the string right-justified in a field of lengthint
. -
string1.split(string2)
returns a list of the substrings ofstring1
that are separated bystring2
. Ifstring2
is omitted, whitespace is used as the separator. -
string.splitlines()
returns a list of the lines instring
, discarding newlines. -
string.startswith(prefix)
returnsTrue
ifstring
starts withprefix
. -
string.strip()
returns a copy ofstring
with all leading and trailing whitespace removed. -
string.upper()
returns a copy ofstring
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.