String methods in Python
A Python provide many built-in string function which can be used based on requirement. A string function allows to perform various operation by using built-in function or methods.

String methods

capitalize()

The string capitalize() method used to convert the first letter of the sentence to uppercase and remaining characters to the lowercase. It returns a string with capitalize character without affecting source string.

casefold()

The string method casefold() used to convert given string to lowercase. It is similar to lower() method but casefold() method is more aggressive and will find more matches when converts more characters into lower case compared to lower() . It mostly used when program include different locales.

center()

The string method center used to center align the string by a specified character (space is default) as the fill character. It creates string of specified length string with specified characters, if character not specified the default value will be empty space. It returns a new string of specified length and filled character.

count()

The string method count() used to find the occurrence of specified value in the given string. It returns a number that specifies that how many times a specified string occur in source string from the given boundary (start and end index) value.

encode()

The string method encode() used to encode the string using specified encoding value. If encoding value not specified, the default value will be UTF-8, will be considered. It returns an encoded string.

endswith()

The string method endswith() used to determine whether the source string ends with specified string. It returns boolean value True, if the source string ends with specified string, otherwise False.

expandtabs()

The string method expandtabs() used to set the tab size to the specified number of whitespaces. It replace the tab characters from the string to the number of spaces in the string. It returns a string that includes the space instead of tab character.

find()

The string method find() used to search the first occurrence of the specified value from the source string. It returns the starting index of the first match, if exists, otherwise -1.

format()

The string method format() used to format string by interpolate value inside the string at specified position. An interpolate string can be define by using curly braces ({}). It returns a string by replacing placeholder with value.

format_map()

The string format_map() used to format string by specifying dictionary object that include key value pair. A specified string interpolate with specified dictionary keys and that replace with respective key's values. That returns string by replacing dictionary key's values.

index()

The string method index() used to search specified search string from the source string. It returns number that represents the index of first match found in source string, otherwise an exception.

isalnum()

The string method isalnum() used to determine whether a string is alpha numeric. It returns boolean value True, if a string is alphanumeric, otherwise False. An alphanumeric includes latter (a-z) and numbers (0-9).

isalpha()

The string method isalpha() used to determine whether a string includes only letters. It returns boolean value True, if all the characters in the string are alphabet letters (a-z), otherwise False.

isascii()

The string method isascii() used to determine whether the characters are ascii characters (a-z, 0-9, symbols, braces etc), it does not include foreign language characters. It returns a boolean value True, if all the characters are ascii characters, otherwise False.

isdecimal()

The string method isdecimal() used to determine whether all the characters are decimals (0-9). It returns boolean value True, if the string all characters as decimal numbers or Unicode characters for decimal number, otherwise False.

isdigit()

The string method isdigit() used to determine whether all the characters are digits. It returns boolean value True, if all the characters are digits, otherwise False.

isidentifier()

The string method isidentifier() used to determine whether a string is a valid identifier. It returns boolean value True, if a string is a valid identifier, otherwise False.

islower()

The string method islower() used to determine whether all the characters are in lowercase. It returns boolean value True, if all the characters are in lowercase, otherwise False.

isnumeric()

The string method isnumeric() used to determine whether all the characters are numeric (0-9). It returns a boolean value true, if all the characters are numeric, otherwise False. It is similar to isdigit() and isdecimal() methods but is allows some special Unicode characters to validate for a number (² and ¾).

isprintable()

The string method isprintable() used to determine whether a string is printable. It returns a boolean value True, if all the characters are printable, otherwise False.

isspace()

The string method isspace() used to determine whether all characters in the string are white spaces. It returns boolean value True, if all the characters are white spaces, otherwise False.

istitle()

The string method istitle() used to determine whether all the words in the string start with uppercase and remaining characters of the word are in lowercase. It returns boolean value True, if string is in title case, otherwise False.

isupper()

The string method isupper() used to determine whether all characters in the string are uppercase. It returns boolean value True, if all characters in the string are uppercase, otherwise False.

join()

The string method join() used to join all iterable items into single string with source string value. It takes iterable items as a parameter, an iterable item must be separated by comma.

ljust()

The string method ljust() used to left align the string with a specified length and character to fill the justify string, if character not specified default character is empty space(' ').

lower()

The string method lower() used to convert all the characters of the string into lowercase letter's. It returns a string where all characters are lower case. It ignore all the number's, symbols or special locale specific characters.

lstrip()

The string method lstrip() method used to trim any left side characters from the string. It removes the specified characters from the left side of the string. If any character not specified to lstrip() function, default value space will be considered.

maketrans()

The string method maketrans() used to create a mapping table that used by translate() method to replace any specified characters with a given character. It store value in the form of key and value. It returns a mapping table that can be used by the translate() method to replace specified characters.

partition()

The string method partition() used to search the specified string and partition the string from the match into tuples. It split the string into three tuple where first item will be part before the matching string, second a matching string and third item will be after the match part. If multiple occurrence found it search for the first occurrence only.

replace()

The string replace() method used to replace the specified phrase with given specified phrase. It replaces all the occurrence in the source string if count value not specified. It returns a string after replacing the occurrence.

rfind()

The string method rfind() used to search an occurrence from the right side of the string (search last occurrence from a string). It returns a number that represents the last occurrence of the search in a string, if exists, otherwise -1.

rindex()

The string method rindex() used to search last occurrence of the specified string from the source string. It returns a number that represents the index of last occurrence. If search not found in the source string, it raises an exception.

rjust()

The string method rjust() used to right align the string by specified length and character as the fill character. If character value not specified, the default value will be considered as space.

rpartition()

The string method rpartition() used to search for the last occurrence of a specified string and splits the string into a tuple from the match and returns a list that contain three elements. The first element string container part before the match, the second element is match string itself and last element is part after the match string.

rsplit()

The string method rsplit() method used to split the source string starting from right to the specified counter value from the specified separator value, if specified, otherwise till the beginning of the source string.

rstrip()

The string method rstrip() used to remove any trailing characters (at the end a string) from the source string, If characters are not specified, the default space character will be removed from the string. It returns string after removing characters.

split()

The string split() method used to split the string into list from the specified separator. It also allows to split specified number of occurrence if more than one occurrence of separator exists. It returns list that contain strings splits from separator, otherwise an empty list. If separator not specified, the default separator will be considered as whitespace.

splitlines()

The string method splitlines() used to split the source string from the newline characters. It returns a list after splitting the string from the newline characters.

startswith()

The string method startswith() used to determine whether a source string starts with specified string. It returns a boolean value True, if the source string start with specified string, otherwise False.

strip()

The string method strip() used to remove leading(at the beginning) and trailing(at the end) characters from the string if present. If characters is not specified, the default space character considered. It returns a string after trimming specified or space character(s) from the string.

swapcase()

The string method swapcase() used to switch the case of letters in the string. It converts uppercase letters into lowercase and lowercase to uppercase. It returns string after switching case of letters. It ignores numbers, symbols. It also swap case for locale specific characters.

title()

The string method title() used to convert a string into title case (convert each word first letter to uppercase). If any word starts with number or symbol, a next character to the number or symbol converted to uppercase. It returns string after converting string to titlecase.

translate()

The string method translate() used to replace a character described in a dictionary or in a mapping table. A string characters which are not described in the dictionary / mapping table will remain same in the source string.

upper()

The string method upper() used to all characters in the string into an uppercase. It returns a string by converting uppercase, excepts number symbol. It also convert locale specific characters into uppercase, if possible.

zfill()

The string method zfill() used to add zeros(0) at the beginning of the string until length of string reaches to the specified values(length). It return string by adding specified zeros. If the value of the length parameter is less than the actual length of the string, it does not add anything to the source string.

decode()

The string method decode() used to decode the string using specified decoded value. If decoding value not specified, the default value will be UTF-8, will be considered. It returns the original string from the encoded string.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us