Previously, we discussed about strings and the various operations on strings. Now, we are going to discuss the Built-in
or the predefined functions on strings.
count function
This function is used to count the occurrences of a character or sub-string in a string.
EXAMPLE
find function
This function is used to find the first index of the string or character in another string. If the function fails to find the desired sub-string or character, it returns -1.
EXAMPLE
rfind function
This function is used to find the last index of the string or character in another string. If the function fails to find the desired sub-string or character, it returns -1.
EXAMPLE
capitalize function
This function is used to convert the first letter of a string to uppercase
and the remaining letters in the string to lowercase
.
EXAMPLE
title function
This function is used to convert the first letter of every word in a string to uppercase
and the remaining letters in the string to lowercase
.
EXAMPLE
lower and upper functions
These functions are used to convert all letters in a string to lowercase
and uppercase
respectively.
EXAMPLE
swapcase function
This function is used to convert all uppercase
letters in a string to lowercase
and vice versa.
EXAMPLE
islower and isupper functions
These functions are used to check whether all letters in a string are lowercase
and uppercase
respectively.
EXAMPLE
istitle function
This function is used to check whether a string is in the title case
or not.
EXAMPLE
replace function
This function is used to replace part of a string by another string. It takes two arguments; first specifying the sub-string to be replaced and the second specifying the sub-string that replaces it.
EXAMPLE
lstrip, rstrip and strip functions
lstrip and rstrip functions are used to remove whitespaces from the beginning and end, respectively.
EXAMPLE
strip function can be used to remove whitespaces from both beginning and end. It can also be used to remove any other characters either from beginning or end from a string passed as an argument in the function.
EXAMPLE
split function
This function is used to split a string into a list of strings based on a delimiter. If no delimiter is specified, python uses whitespaces as default.
EXAMPLE
partition function
This function is used to divide a string into two parts based on a delimiter and return a tuple comprising of string before delimiter
, delimiter
itself and string after delimiter
.
EXAMPLE
join function
This function is used to join a sequence of strings.
EXAMPLE
isspace, isalpha, isdigit and isalnum functions
These functions are used to check whether a string comprises of whitespaces, alphabets, digits or alpha-numerics only. They return boolean values upon evaluating.
EXAMPLE
startswith and endswith functions
These functions are used to check whether a string starts
or ends
with a particular string. They return boolean values upon evaluating.
EXAMPLE
encode and decode functions
These functions are used to encode and decode strings on the basis of a given coding scheme.
EXAMPLE
That's it for strings. Hope you guys are following and keeping up the practice!!