This article was published as a part of the Data Science Blogathon
Python string is a built-in type sequence. Strings can be used to handle textual data in Python. Python Strings are immutable sequences of Unicode points. Creating Strings is the simplest and easy to use in Python. To create a string in Python, we simply enclose a text in single as well as double-quotes. Python treats both single and double quotes statements the same. So, In this article, we will be discussing some important and useful Functions of Strings in Python for Data Analysis and Data Manipulation, mainly used in Natural Language Processing(NLP).
To follow this article properly, I assume you are familiar with the basics of Python. If not, I recommend the below popular course given by Analytics Vidhya to get started with the Python Basics:
Image Source: Link
The Python String Functions which we are going to discuss in this article are as follows:
Image Source: Link
The capitalize() function returns a string where the first character is the upper case.
Python Code:
#You can see the output of other functions as well
string = "analytics Vidhya is the Largest data science Community"
print(string.capitalize())
string = '10 version of Data Science Blogathon by Analytics Vidhya is very good' print(string.capitalize())
Output:
10 version of data science blogathon by analytics vidhya is very good
The lower() function returns a string where all the characters in a given string are lower case. This function doesn’t do anything with Symbols and Numbers i.e, simply ignored these things.
Syntax: string.lower()
string = "Analytics Vidhya is the Largest Data Science Community" print(string.lower())
Output:
analytics vidhya is the largest data science community
string = '10 version of Data Science Blogathon by Analytics Vidhya is very good' print(string.lower())
Output:
10 version of data science blogathon by analytics vidhya is very good
The title() function returns a string where the first character in every word of the string is an upper case. It is just like a header, or a title.
If in a string any of the words contain either a number or a symbol, then this function converts the first letter after that to upper case.
Syntax: string.title()
string = "analytics vidhya is the Largest data science Community" print(string.title())
Output:
Analytics Vidhya Is The Largest Data Science Community
string = '10th version of Data Science Blogathon by Analytics Vidhya is very good' print(string.title())
Output:
10Th Version Of Data Science Blogathon By Analytics Vidhya Is Very Good
The casefold() function returns a string where all the characters are lower case.
This function is similar to the lower() function, but the casefold() function is stronger, more aggressive, meaning that it will convert more characters into lower case and will find more matches when comparing two strings and both are converted using the casefold() function.
Syntax: string.casefold()
string = "Analytics Vidhya is the Largest Data Science Community" print(string.casefold())
Output:
analytics vidhya is the largest data science community
string = '10th version of Data Science Blogathon by Analytics Vidhya is very good' print(string.casefold())
Output:
10th version of data science blogathon by analytics vidhya is very good
The upper() function returns a string where all the characters in a given string are in the upper case. This function doesn’t do anything with Symbols and Numbers i.e, simply ignored these things.
Syntax: string.upper()
string = "analytics Vidhya is the Largest Data Science Community" print(string.upper())
Output:
ANALYTICS VIDHYA IS THE LARGEST DATA SCIENCE COMMUNITY
string = '10th version of Data Science Blogathon by Analytics Vidhya is very good' print(string.upper())
Output:
10TH VERSION OF DATA SCIENCE BLOGATHON BY ANALYTICS VIDHYA IS VERY GOOD
The count() function finds the number of times a specified value(given by the user) appears in the given string.
Syntax: string.count(value, start, end)
string = "analytics Vidhya is the Largest Analytics Community" print(string.count("analytics"))
Output:
1
string = "analytics Vidhya is the Largest analytics Community" print(string.count("analytics", 10, 18))
Output:
0
The find() function finds the first occurrence of the specified value. It returns -1 if the value is not found in that string.
The find() function is almost the same as the index() function, but the only difference is that the index() function raises an exception if the value is not found.
Syntax: string.find(value, start, end)
string = "analytics vidhya is the Largest data science Community" print(string.find("d"))
Output:
12
string = "analytics vidhya is the Largest data science Community" print(string.find("d", 5, 16))
Output:
12
string = "analytics vidhya is the Largest data science Community" print(string.find("d", 5, 10))
Output:
-1
The replace() function replaces a specified phrase with another specified phrase.
Note: All occurrences of the specified phrase will be replaced if nothing else is specified.
Syntax: string.replace(oldvalue, newvalue, count)
string = "analytics vidhya is the Largest data science Community" print(string.replace("science", "scientists"))
Output:
analytics vidhya is the Largest data scientists Community
string = "Data science Courses by analytics vidhya are the best courses to learn Data science" print(string.replace("science", "scientists", 1))
Output:
Data scientists Courses by analytics vidhya are the best courses to learn Data science
The swapcase() function returns a string where all the upper case letters are lower case and vice versa.
Syntax: string.swapcase()
string = "analytics vidhya is the Largest data science Community" print(string.swapcase())
Output:
ANALYTICS VIDHYA IS THE lARGEST DATA SCIENCE cOMMUNITY
string = '10th version of Data Science Blogathon by Analytics Vidhya is very good' print(string.swapcase())
Output:
10TH VERSION OF dATA sCIENCE bLOGATHON BY aNALYTICS vIDHYA IS VERY GOOD
The join() function takes all items in an iterable and joins them into one string. We have to specify a string as the separator.
Syntax: string.join(iterable)
myTuple = ("Data Scientists", "Machine Learning", "Data Science") x = "#".join(myTuple) print(x)
Output:
Data Scientists#Machine Learning#Data Science
myDict = {"name": "Analytics Vidhya", "country": "India", "Technology": "Data Science"} mySeparator = "TEST" x = mySeparator.join(myDict) print(x)
Output:
nameTESTcountryTESTTechnology
You can also check my previous blog posts.
Previous Data Science Blog posts.
Here is my Linkedin profile in case you want to connect with me. I’ll be happy to be connected with you.
For any queries, you can mail me on [email protected]
Thanks for reading!
I hope that you have enjoyed the article. If you like it, share it with your friends also. Something not mentioned or want to share your thoughts? Feel free to comment below And I’ll get back to you. 😉
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.