10 Useful Python String Functions Every Data Scientist Should Know About!

Chirag Goyal Last Updated : 12 Nov, 2024
6 min read

This article was published as a part of the Data Science Blogathon

Introduction

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:

Python for Data Science

Tutorial For Beginners | Free Tutorials | FITA Academy | String Functions

                                                          Image Source: Link

Python String Functions

The Python String Functions which we are going to discuss in this article are as follows:

  • capitalize( ) function
  • lower( ) function
  • title( ) function
  • casefold( ) function
  • upper( ) function
  • count( ) function
  • find( ) function
  • replace( ) function
  • swapcase( ) function
  • join( ) function
Me Strings Are Actually an Array of Characters Python Well Yesbut Actually No sTrInGs aRe iMmUtAbLe | Programmer Humor Meme on ME.ME| String Functions

                                                   Image Source: Link

 

capitalize( ) function

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())

Example 2: What happens if the first character is a number instead of a character

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

lower( ) function

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()

Example 1: Lower case the given string

string = "Analytics Vidhya is the Largest Data Science Community"
print(string.lower())

Output:

analytics vidhya is the largest data science community

Example 2: What happens if there is a number instead of a character

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

title( ) function

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()

Example 1: Make the first letter in each word upper case

string = "analytics vidhya is the Largest data science Community"
print(string.title())

Output:

Analytics Vidhya Is The Largest Data Science Community

Example 2: What happens if there is a number instead of a character

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

casefold( ) function

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()

Example 1: Make the given string to lower case

string = "Analytics Vidhya is the Largest Data Science Community"
print(string.casefold())

Output:

analytics vidhya is the largest data science community

Example 2: What happens if there is a number instead of a character

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

upper( ) function

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()

Example 1: Upper case the given string

string = "analytics Vidhya is the Largest Data Science Community"
print(string.upper())

Output:

ANALYTICS VIDHYA IS THE LARGEST DATA SCIENCE COMMUNITY

Example 2: What happens if there is a number instead of a character

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

count( ) function

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)

Example 1: Return the number of times the value “analytics” appears in the string

string = "analytics Vidhya is the Largest Analytics Community"
print(string.count("analytics"))

Output:

1

Example 2: Return the number of times the value “analytics” appears in the string from position 10 to 18

string = "analytics Vidhya is the Largest analytics Community"
print(string.count("analytics", 10, 18))

Output:

0

find( ) function

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)

Example 1: Where in the text is the first occurrence of the letter “d”?

string = "analytics vidhya is the Largest data science Community"
print(string.find("d"))

Output:

12

Example 2: Where in the text is the first occurrence of the letter “d” when you only search between positions 5 and 16?

string = "analytics vidhya is the Largest data science Community"
print(string.find("d", 5, 16))

Output:

12

Example 3: If the value is not found, the find() function returns -1, but the index() function will raise an exception

string = "analytics vidhya is the Largest data science Community"
print(string.find("d", 5, 10))

Output:

-1

replace( ) function

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)

Example 1: Replace all occurrences of the word “science”

string = "analytics vidhya is the Largest data science Community"
print(string.replace("science", "scientists"))

Output:

analytics vidhya is the Largest data scientists Community

Example 2: Replace only the first occurrence of the word “science”

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

swapcase( ) function

The swapcase() function returns a string where all the upper case letters are lower case and vice versa.

Syntax: string.swapcase()

Example 1: Make the lower case letters to upper case and the upper case letters to lower case

string = "analytics vidhya is the Largest data science Community"
print(string.swapcase())

Output:

ANALYTICS VIDHYA IS THE lARGEST DATA SCIENCE cOMMUNITY

Example 2: What happens if there is a number instead of a character

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

join( ) function

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)

Example 1: Join all the items in a given tuple into a string, using a # (hashtag) character as the separator

myTuple = ("Data Scientists", "Machine Learning", "Data Science")
x = "#".join(myTuple)
print(x)

Output:

Data Scientists#Machine Learning#Data Science

Example 2: Join all the items in a given dictionary into a string, using the word “TEST” as the separator

myDict = {"name": "Analytics Vidhya", "country": "India", "Technology": "Data Science"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)

Output:

nameTESTcountryTESTTechnology

Other Blog Posts by Me

You can also check my previous blog posts.

Previous Data Science Blog posts.

LinkedIn

Here is my Linkedin profile in case you want to connect with me. I’ll be happy to be connected with you.

Email

For any queries, you can mail me on [email protected]

End Notes

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.

I am a B.Tech. student (Computer Science major) currently in the pre-final year of my undergrad. My interest lies in the field of Data Science and Machine Learning. I have been pursuing this interest and am eager to work more in these directions. I feel proud to share that I am one of the best students in my class who has a desire to learn many new things in my field.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details