- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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() 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 −
The len() function returns the length of the string.your_string is the string whose length you want to find out.
string = "This is your string" print(len(string))
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 −
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.
string = "ThIs is yOuR stRinG" print(string.upper()) # convert string into upper case print(string.lower()) # converts string to lowercase
THIS IS YOUR STRING this is your string
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 −
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.
string = "This is your string" print(string.strip()) # remove leading and trailing whitespace from the string
This is your string
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 −
string = "Hello, Welcome to , Tutorials Point" print(string.split(","))
['Hello', ' Welcome to ', ' Tutorials Point']
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 −
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.
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))
Hello Welcome to Tutorials Point !
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 −
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.
string = "Hello, World!" print(string.replace("World", "Python"))
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 −
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.
string = "Hello, World!" print(string.find("World"))
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().
Kickstart Your Career
Get certified by completing the courseGet Started