Learn everything about Analytics

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

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

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.

Syntax: string.capitalize()

Example 1: Make the first letter upper case in the given sentence

string = "analytics Vidhya is the Largest data science Community"
print(string.capitalize())

Output:

Analytics vidhya is the largest data science community

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 Gmail.

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.

You can also read this article on our Mobile APP Get it on Google Play