7 Useful String Function in Python


String is a data type in python which is widely used for data manipulation and analysis in machine learning and data analytics. Python is used in almost every technological development like web development, app development, desktop app development, game development, machine learning, artificial intelligence and data analysis etc. Python provides various inbuilt string functions that can be used to manipulate and process string data. In this article we will discuss 7 useful string functions in Python.

len()

len() function is used to find the length of the string. It returns a number which specifies the number of characters in the string including spaces and special characters. The syntax for using len() function is −

Syntax

len(your_string)

The len() function returns the length of the string.your_string is the string whose length you want to find out.

Example

string = "This is your string"
print(len(string))

Output

19

upper() and lower()

upper() and lower() functions are used to convert the string to uppercase and lowercase respectively. These functions are widely used in backend and frontend user forms implementation to keep the string entered by the user in lowercase or uppercase as required. The syntax for using upper case and lower case are −

Syntax

string.upper()
string.lower()

upper() function converts each character in the string to uppercase and lower() function converts each character to lowercase.string in the above code is the string you need to convert to upper or lower case.

Example

string = "ThIs is yOuR stRinG"
print(string.upper()) # convert string into upper case
print(string.lower()) # converts string to lowercase

Output

THIS IS YOUR STRING
this is your string

strip()

strip() function is used to remove any leading or trailing whitespace in a string. Sometimes when doing data cleaning in machine learning projects we need to remove the leading and trailing whitespace from the string then we use the strip function. The strip function returns a new string without any white spaces. The syntax for using the strip function is −

Syntax

string.strip()

The strip() function returns the the string without and leading or trailing whitespace Here string here is the string from which you need to remove the leading or trailing whitespace.

Example

string = "This is your string"
print(string.strip()) # remove leading and trailing whitespace from the string 

Output

This is your string

split()

split() is also a commonly used function which is used to split a string in multiple substring based on the passed delimiter. The syntax for using the split function is as follows −

Syntax

string.split(delimiter)

Example

string = "Hello, Welcome to , Tutorials Point"
print(string.split(","))

Output

['Hello', ' Welcome to ', ' Tutorials Point']

join()

Join function in python is used to join a list of strings into a single string.The string is joined using a delimiter which we have to specify while using the join function.The syntax for using the join function is as follows −

Syntax

delimiter.join(list)

The join() function joins the words in the list to form a complete sentence. The delimiter is the special character which is used to identify the joining point of the strings.list of strings is passed as an argument to the join function.

Example

Here, list is the list of multiple strings which is joined using join function to convert it into a single string with delimiter in between the strings.

list = ['Hello', 'Welcome', 'to', 'Tutorials', 'Point', '!']
delimiter = " "
print(delimiter.join(list))

Output

Hello Welcome to Tutorials Point !

replace()

replace() function is used to replace a substring in a string with another substring. This is also a commonly used function which is used while cleaning data. We have to pass the old string which is to be replaced and a new string which needs to be added as a parameter to replace() function. The syntax for using replace function is −

Syntax

string.replace(old_string,new_string)

replace() function replaces the old substring in a string with a new substring. Here old_string is the string which needs to be replaced with new_string.

Example

string = "Hello, World!"
print(string.replace("World", "Python"))

Output

Hello, Python!

find()

Find() function is used to find the first occurrence of a substring in a string. It returns the index of the first occurrence of the substring. If the substring is not found it will return -1. The syntax for using find() function is −

Syntax

string.find(substring)

The find() function finds the index of the first appearance of the searched string. The substring to be searched is passed as an argument to the find function.

Example

string = "Hello, World!"
print(string.find("World"))

Output

7 

Conclusion

In this article we understood some commonly used python string functions which are used for string manipulation. There are many string functions which can be easily found in the python documentation. Some of the string functions which we explored were len(),upper(),lower(),split(),strip(),join(),replace(),find().

Updated on: 13-Apr-2023

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements