How to Perform Sentiment Analysis in Python Using TextBlob

Alexandros Pappas
2 min readDec 2, 2022

--

Sentiment analysis is a powerful tool in the field of natural language processing (NLP) that allows us to automatically identify and extract subjective information from text. In this tutorial, we will go over how to perform sentiment analysis in Python using the TextBlob library.

To start, you will need to install TextBlob by running the following command:

pip install textblob

Once you have installed TextBlob, you can start using it to perform sentiment analysis. The first step is to create a TextBlob object from the text you want to analyze. You can do this by passing the text as a string to the TextBlob constructor:

from textblob import TextBlob

text = "This is a great tutorial on sentiment analysis!"
blob = TextBlob(text)

Now that we have a TextBlob object, we can use the sentiment property to get the sentiment of the text. The sentiment property returns a tuple containing the polarity and subjectivity of the text. The polarity is a float value in the range [-1, 1] where -1 represents very negative sentiment, 0 represents neutral sentiment, and 1 represents very positive sentiment. The subjectivity is a float value in the range [0, 1] where 0 represents objective text and 1 represents highly subjective text.

sentiment = blob.sentiment
print(sentiment)

The output of the above code will be a tuple containing the polarity and subjectivity of the text, like this:

Sentiment(polarity=1.0, subjectivity=0.75)

In this case, the polarity of the text is 1.0, which indicates a very positive sentiment, and the subjectivity is 0.75, which indicates that the text is highly subjective.

You can also use the polarity and subjectivity properties to get the polarity and subjectivity of the text separately:

polarity = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity

print(polarity)
print(subjectivity)

The output of the above code will be the polarity and subjectivity of the text, like this:

1.0
0.75

In addition to getting the sentiment of individual pieces of text, you can also use TextBlob to perform sentiment analysis on a whole document. To do this, you can create a TextBlob object from the entire document and then use the sentiment property to get the overall sentiment of the document.

from textblob import TextBlob

document = "This is a great tutorial on sentiment analysis! I really enjoyed it and learned a lot."
blob = TextBlob(document)
sentiment = blob.sentiment
print(sentiment)

The output of the above code will be the overall sentiment of the document, like this:

Sentiment(polarity=0.75, subjectivity=0.725)

As you can see, TextBlob makes it easy to perform sentiment analysis in Python. With just a few lines of code, you can quickly and easily identify the sentiment of individual pieces of text or entire documents. This can be a powerful tool for a wide range of applications, including social media analysis, customer service, and more.

--

--

Alexandros Pappas

A software engineer passionate about Graph Theory and its applications.