Tutorialsteacher

Follow Us

Built-in String Functions in Python

The following table lists all the functions that can be used with the string type in Python 3.

MethodDescription
capitalize()Returns the copy of the string with its first character capitalized and the rest of the letters are in lowercased.
casefold()Returns a lowered case string. It is similar to the lower() method, but the casefold() method converts more characters into lower case.
center()Returns a new centered string of the specified length, which is padded with the specified character. The deafult character is space.
count()Searches (case-sensitive) the specified substring in the given string and returns an integer indicating occurrences of the substring.
endswith()Returns True if a string ends with the specified suffix (case-sensitive), otherwise returns False.
expandtabs()Returns a string with all tab characters \t replaced with one or more space, depending on the number of characters before \t and the specified tab size.
find()Returns the index of the first occurence of a substring in the given string (case-sensitive). If the substring is not found it returns -1.
index()Returns the index of the first occurence of a substring in the given string.
isalnum()Returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.
isalpha()Returns True if all characters in a string are alphabetic (both lowercase and uppercase) and returns False if at least one character is not an alphabet.
isascii()Returns True if the string is empty or all characters in the string are ASCII.
isdecimal()Returns True if all characters in a string are decimal characters. If not, it returns False.
isdigit()Returns True if all characters in a string are digits or Unicode char of a digit. If not, it returns False.
isidentifier()Checks whether a string is valid identifier string or not. It returns True if the string is a valid identifier otherwise returns False.
islower()Checks whether all the characters of a given string are lowercased or not. It returns True if all characters are lowercased and False even if one character is uppercase.
isnumeric()Checks whether all the characters of the string are numeric characters or not. It will return True if all characters are numeric and will return False even if one character is non-numeric.
isprintable()Returns True if all the characters of the given string are Printable. It returns False even if one character is Non-Printable.
isspace()Returns True if all the characters of the given string are whitespaces. It returns False even if one character is not whitespace.
istitle()Checks whether each word's first character is upper case and the rest are in lower case or not. It returns True if a string is titlecased; otherwise, it returns False. The symbols and numbers are ignored.
isupper()Returns True if all characters are uppercase and False even if one character is not in uppercase.
join()Returns a string, which is the concatenation of the string (on which it is called) with the string elements of the specified iterable as an argument.
ljust()Returns the left justified string with the specified width. If the specified width is more than the string length, then the string's remaining part is filled with the specified fillchar.
lower()Returns the copy of the original string wherein all the characters are converted to lowercase.
lstrip()Returns a copy of the string by removing leading characters specified as an argument.
maketrans()Returns a mapping table that maps each character in the given string to the character in the second string at the same position. This mapping table is used with the translate() method, which will replace characters as per the mapping table.
partition()Splits the string at the first occurrence of the specified string separator sep argument and returns a tuple containing three elements, the part before the separator, the separator itself, and the part after the separator.
replace()Returns a copy of the string where all occurrences of a substring are replaced with another substring.
rfind()Returns the highest index of the specified substring (the last occurrence of the substring) in the given string.
rindex()Returns the index of the last occurence of a substring in the given string.
rjust()Returns the right justified string with the specified width. If the specified width is more than the string length, then the string's remaining part is filled with the specified fill char.
rpartition()Splits the string at the last occurrence of the specified string separator sep argument and returns a tuple containing three elements, the part before the separator, the separator itself, and the part after the separator.
rsplit()Splits a string from the specified separator and returns a list object with string elements.
rstrip()Returns a copy of the string by removing the trailing characters specified as argument.
split()Splits the string from the specified separator and returns a list object with string elements.
splitlines()Splits the string at line boundaries and returns a list of lines in the string.
startswith()Returns True if a string starts with the specified prefix. If not, it returns False.
strip()Returns a copy of the string by removing both the leading and the trailing characters.
swapcase()Returns a copy of the string with uppercase characters converted to lowercase and vice versa. Symbols and letters are ignored.
title() Returns a string where each word starts with an uppercase character, and the remaining characters are lowercase.
translate()Returns a string where each character is mapped to its corresponding character in the translation table.
upper()Returns a string in the upper case. Symbols and numbers remain unaffected.
zfill()Returns a copy of the string with '0' characters padded to the left. It adds zeros (0) at the beginning of the string until the length of a string equals the specified width parameter.