Built-in string functions in python

Built-in string functions in python

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

image.png

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

image.png

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

image.png

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

image.png

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

image.png

lower and upper functions

These functions are used to convert all letters in a string to lowercase and uppercase respectively.

EXAMPLE

image.png

swapcase function

This function is used to convert all uppercase letters in a string to lowercase and vice versa.

EXAMPLE

image.png

islower and isupper functions

These functions are used to check whether all letters in a string are lowercase and uppercase respectively.

EXAMPLE

image.png

istitle function

This function is used to check whether a string is in the title case or not.

EXAMPLE

image.png

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

image.png

lstrip, rstrip and strip functions

lstrip and rstrip functions are used to remove whitespaces from the beginning and end, respectively.

EXAMPLE

image.png 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

image.png

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

image.png

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

image.png

join function

This function is used to join a sequence of strings.

EXAMPLE

image.png

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

image.png

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

image.png

encode and decode functions

These functions are used to encode and decode strings on the basis of a given coding scheme.

EXAMPLE

image.png

That's it for strings. Hope you guys are following and keeping up the practice!!