Python String Methods

Python String Methods

Python has a set of built-in string methods. The Python string methods are as follows:

capitalize() The string.capitalize() method returns a copy of the string by converting the first character to upper case. 
center() The string.center() method returns a centered in a string of length width.
count() This method returns the number of times a specified value occurs in a string.
decode() The string.decode() method decodes the string using the codec registered for encoding.
encode() The string.encode() method returns an encoded version of the string
endswith() The string.endswith() method returns a boolean value true if the specified string ends with the given value.
expandtabs() This method returns a copy of the string where all tab characters are expanded using spaces.
find() The string.find () method returns the lowest index in the string where the substring is found else it returns -1.
index() The string.index() method returns the lowest index in the string where the substring is found else it raises a ValueError.
isalnum() This method returns a Boolean value True if all characters in the string are alphanumeric
isalpha() The string.isalpha() method returns True if all characters in thespecified  string are in the alphabet.
isdigit() The string.isdigit() method returns True if all characters in the string are in digits.
islower() The string.islower() method returns a Boolean value True if all characters in the specified string are in lower case.
isspace() The string.isspace() method returns true if there are only whitespace characters in the string else it returns false.
istitle() The string.istitle() method returns true if the string is a titlecased string and follows the rules of a tittle.
isupper() The string.isupper() method returns true if all cased characters in the string are uppercase else it returns false.
join() This method joins the elements of an iterable to the end of the string
ljust() The string.ljust() method returns a left justified version of the string.
lower() The string.lower() method returns a copy of the string converted to lowercase.
lstrip() This method returns a copy of the string with leading characters removed.
replace() The string.replace() method returns a string where a specified value is replaced with the old value.
rfind() The string.rfind() method returns the highest index in the string if the substring sub is found else it raises a ValueError.
rindex() This method return the highest index in the string if the substring is found else it returns -1.
rjust() The string.rjust() method returns a right justified version of the specified string of length width.
rsplit() This method returns a list of words in the string by splitting the string at the specified separator.
rstrip() The string.rstrip() method returns a right trim version of the string
split() The string.split() method splits the specified string at separators, and returns the list.
splitlines() This method splits the given string at line breaks and returns the list.
startswith() The string.startswith() returns a Boolean value true if the specified string starts with the given prefix, else it returns False.
strip() The string.strip() method returns a copy of the string by removing the  leading and trailing characters. 
swapcase() This method returns a copy of the string with uppercase characters converted to lowercase and vice versa.
title() The string.title() method returns the titles cased string by converting the first character of each word to upper case.
translate() The string.translate() method returns a copy of the translated string.
upper() The string.upper() method returns a copy of the string converted to uppercase.
zfill() The string.zfill() method returns the numeric string left filled with a specified number of 0 values at the beginning.

Example 1

Output

Example 2

Output

Example 3

Output