Python String Functions

Python has built-in functions for almost every operation that is to be performed on a string. To make it simple, these are classified on the basis of the frequency of their use as well as their operations. They are as follows :

Python String Functions Classification

  • Basic Functions
  • Advanced Functions
  • Miscellaneous (These functions are not specifically for strings but they can be used in string manipulation)

Basic String Functions

capitalize()It converts the first character of a string to uppercasestr_name.capitalize()
casefold()It converts any string to lower case irrespective of its casestr_name.casefold()
center()It is used to center align the stringstr_name.center(Length,character)
count()It is used to count the number of times a specific value appears in the stringstr_name.count(value,start,end)
endswith()It checks if the string is ending with specified value then it returns Truestr_name.endswith(value,start,end)
find()It is used to find the presence of a specified value in a stringstr_name.find(value,start,end)
index()It is used to find the first occurrence of a specified value in the stringstr_name.index(value,start,end)
isalnum()It checks if all the characters are alphanumeric then returns Truestr_name.isalnum()
isalpha()It checks if all the characters are alphabet (a-z) then returns Truestr_name.isalpha()
isdecimal()It checks if all the characters are decimals (0-9) then returns Truestr_name.isdecimal()
isdigit()It checks if all the characters are digits then returns Truestr_name.isdigit()
islower()It checks if all the characters are in lowercase then returns Truestr_name.islower()
isnumeric()It checks if all the characters are numeric (0-9) then returns Truestr_name.isnumeric()
isspace()It checks if all the characters are whitespaces then returns Truestr_name.isspace()
isupper()It checks if all the characters are in uppercase then returns Truestr_name.isupper()
lower()It is used to convert all characters to lowercasestr_name.lower()
partition()It is used to split the string into a tuple of three elementsstr_name.partition(value)
replace()It is used to replace specified word or phrase into another word or phrase in the stringstr_name.replace(oldvalue,newvalue,count)
split()It is used to split a string into a liststr_name.split(separator,maxsplit)
splitlines()It is used to split the string and make a list of it. Splits at the line breaks.str_name.splitlines(keeplinebreaks)
startswith() It checks if the string is starting with specified value then it returns Truestr_name.startswith(value,start,end)
strip()It is used to remove characters specified in argument from both the endsstr_name.strip(characters)
swapcase()It is used to swap uppercase string to lowercase or vice versastr_name.swapcase()
title()It converts initial letter of each word to uppercasestr_name.title()
upper()It is used to convert all characters in a string to uppercasestr_name.upper()

Advanced Python String Functions

encode()It is used to return encoded stringsstr_name.encode(encoding=encoding, errors=errors)
expandtabs()It is used to set or fix tab spaces between characters or alphabetsstr_name.expandtabs(tabsize)
format()It replaces variable name written within {} with the value at executionstr_name.format(value1,value2...)
format_map()It is used to format a string given and is returnedstr_name.format_map(mapping)
isidentifier()It checks if characters are alphanumeric letters (a-z) and (0-9), or underscores (_) and return Truestr_name.isidentifier()
isprintable()It checks if all characters are printable then returns Truestr_name.isprintable()
istitle()It checks if all initial characters of words are in uppercase then returns Truestr_name.istitle()
join()It accepts words as iterable and joins them into a stringstr_name.join(iterable)
ljust()It returns a left-aligned string with the minimum value given as widthstr_name.ljust(length,character)
lstrip()It removes characters from left end based on the given argumentstr_name.lstrip(characters)
maketrans()It creates a mapped table usable for translationsstr_name.maketrans(x,y,z)
rsplit()It is used to split the string from the right endstr_name.rsplit(separator,maxsplit)
rfind()It searches for a specified value and finds the position of its last valuestr_name.rfind(value,start,end)
rindex() It searches for a specified value and finds the position of its last valuestr_name.rindex(value,start,end)
rjust() It returns a right-aligned string with the minimum value given as widthstr_name.rjust(length,character)
rpartition()It looks for the last occurrence of a specified string and splits the string into the tuple of three elementsstr_name.rpartition(value)
rstrip() It removes characters from the right end based on the given argumentstr_name.rstrip(characters)
translate()It is used to get a translated stringstr_name.translate(table)
zfill()It returns a new string with ‘0’ characters padded to the left on the stringstr_name.zfill(len)

Miscellaneous Functions that work on String

ascii()It returns a string containing the printable form of an object and ignores the non-ASCII values in the stringascii(object)
bool()It returns boolean value i.e. True or False for an objectbool(value)
bytearray()It returns an object containing an array of bytes provided through the inputbytearray(source,encoding,errors)
bytes()It returns bytes object which cannot be modified and is a sequence of integers in the range from 0 to 255bytes(source,encoding,errors)
enumerate()It is used to add a counter to an iterable and then returns its valueenumerate(iterable,start=0)
float()It returns floating-point number from the given argumentfloat(argument)
hash()It returns the hash value of the object, if applicablehash(object)
id()It returns the specific identity of an object which is a unique integerid(object)
int()It returns an integer object from the given input and the base of the returned object will always be 10int(x=0,base=10)
len()It returns the length of sequence i.e. number of items in an objectlen(sequence)
map()It is used to apply a given function to every item of iterable which can be a tuple, list, etc. and also returns a list containing resultant valuesmap(function, iterable, ...)
ord()It accepts a string argument of single Unicode character and returns its respect Unicode pointord(character)
print()It prints the provided object to any output device print(object(s),separator=separator, end=end,file=file,flush=flush)
slice()It creates an object which represents a set of indices specified by its range(start, stop, step)slice(stop)
slice(start,stop,step)
type()It returns the object’s typetype(object)
type(name,bases,dict)

References

Python Official Documentation