Python Built-in String Methods and Functions

Python Supports many built-in methods to manipulate strings. A method is just like a function. Strings are an example of Python objects. An object is an entity that contains both data (the actual string itself) as well as functions to manipulate that data. These functions are available to any instance (variable) of the object.
Some of the most commonly used Built-in String Methods are given as follows :

FunctionDescription
capitalize()It is used to capitalize the first letter of the string.
center(width, fillchar)It returns a string with the original string centred to a total of width columns and filled with fillchar in columns that don't have characters.
count(str, beg, end)It counts the number of times str occurs in a string.
startswith(suffix, beg, end)It checks if string starts with prefix, if so, it returns True and False otherwise.
endswith(suffix, beg, end)It checks if string ends with suffix, it returns True if so and False otherwise.
find(str, beg, end)It checks if str is present in string. If found, it returns the position at which str occurs in string, otherwise returns -1.
rfind(str, beg, end)It also checks if str present in string but it starts searching from the end.
index(str, beg, end)It's just like find but raises an exception if str isn't found.
rindex(str, beg, end)It is also like index but it starts searching from the end and raises an exception if str isn't found.
isalnum()It returns True if string has at least one character and every character is either a number or an alphabet and False otherwise.
isalpha()It returns True if string has at least one character and every character is an alphabet and False otherwise.
isdigit()It returns True if string contains only digits and False otherwise.
islower()It returns True if string has at least one character and every character is a lowercase alphabet and False otherwise.
isspace()It returns True if string contains only white-space characters and False otherwise.
isupper()It returns True if string has at least one character and every character is an uppercase alphabet and False otherwise.
len(string)It returns the length of the string.
ljust(width[, fillchar])It returns a string left-justified to a total of width columns. Columns without characters are padded with the character specified in the fillchar argument.
rjust(width[, fillchar])It returns a string right-justified to a total of width columns. Columns without characters are padded with the character specified in the fillchar argument.
zfill(width)It returns string left padded with zeros to a total of width characters. It is used with numbers and it also retains its sign (+ or -)
upper()It converts all characters in the string into uppercase.
lower()It converts all characters in the string into lowercase.
lsrip()It removes all leading whitespace in string.
rstrip()It removes all trailing white-space in string.
strip()It removes all leading and trailing white-space in string.
max(str)It returns the highest alphabetical character from the string str.
min(str)It returns the lowest alphabetical character from the string str.
replace(old, new[, max])It replaces all or max occurrences of old in string with new.
title()It returns string in title case.
join(list)This function is used to join a list of strings.
swapcase()It toggles the case of every character (uppercase character becomes lowercase and vice-versa)
split(delim)It returns a list of sub-strings separated by the specified delimiter.
isidentifier()It returns True if the string is a valid identifier.
enumerate(str)It returns an enumerate object that lists the index and value of all the characters in the string as pairs.

There are two important functions are:
1. format()
2. splitlines()

format():

The format() used with strings is a very versatile and powerful function used for formatting strings. Format strings have curly braces {} as placeholders or replacement fields which gets replaced.
Example:

str1="{}, {} and {}".format('Sun', 'Moon', 'Stars')
print("\n The default sequence of arguments are: " +str1)
str1="{1}, {0} and {2}".format('Sun', 'Moon', 'Stars')
print("\n The positional sequence of arguments (1, 0 and 2) are: " +str2)
str3="{c}, {b} and {a}".format(a='Sun', b='Moon', c='Stars')
print("\n The keyword sequence of arguments are: " +str3)

Output:

The default sequence of arguments are: Sun, Moon and Stars

The positional sequence of arguments (1, 0 and 2) are: Moon, Sun and Stars

The keyword sequence of arguments are: Stars, Moon and Sun

splitlines():

The splitlines() returns a list of the lines in the string. This method uses the newline characters like \r or \n to split lines. Line breaks aren’t included in the resulting list unless keep ends is given as True. It has the following syntax:

str.splitlines([keepends])

Example:

print('Sun and \n\n Stars, Planets \r and Moon\r\n'.splitlines())
print('Sun and \n\n Stars, Planets \r and Moon\r\n'.splitlines(True))

Output:

['Sun and ', '', ' Stars, Planets ', ' and Moon']
['Sun and \n', '\n', ' Stars, Planets \r', ' and Moon\r\n']