String Methods

In this list of common string methods, S is the string to which the method is applied, and s and t are other strings.

OperationDescription
S.capitalize()    If the first character of S is a letter, capitalize it
S.count(s)Count the number of times s occurs in S
S.endswith(s)True if S ends with s; False otherwise
S.find(s)Return the index of the first occurrence of s in S; −1 if s not in S
S.index(s)Return the index of the first occurrence of s in S; ValueError exception if s not in S
S.isalnum()True if S contains only alphanumerics (letters and digits); False otherwise
S.isalpha()True if S contains only alphabetics (letters); False otherwise
S.isdigit()True if S contains only digits; False otherwise
S.islower()True if all letters in S are lower case; False otherwise
S.isspace()True if S contains only white space; False otherwise
S.isupper()True if all letters in S are upper case; False otherwise
S.lower()Change all upper case letters in S to lower case
S.lstrip()Delete all leading white space from S and return the result
S.replace(s,t)Replace all occurrences of s with t in S
S.rfind(s)Return the index of the last occurrence of s in S; −1 if s not in S
S.rindex(s)Return the index of the last occurrence of s in S; ValueError exception if s not in S
S.rstrip()Delete all trailing white space from S
S.strip()Delete all leading and trailing white space from S
S.swapcase()Change all upper case letters in S to lower case and all lower case letters to upper case
S.title()Capitalize each word in S
S.upper()Change all lower case letters in S to upper case

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
MHI 289I, Programming in Health Informatics
Version of October 6, 2025 at 4:00PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh