How to Count Words in a String in Python

Strings are essential data types in any programming language, including python. We need to perform many different operations, also known as string preprocessing like removing the unnecessary spaces, counting the words in a string, making the string in the same cases (uppercase or lowercase). In this article, we will learn how to count words in a string in python. 

We will learn how to count the number of words in a string. For example- We have a string-” Hello, this is a string.” It has five words. Also, we will learn how to count the frequency of a particular word in a string. 

Different Ways in Python to count words in a string 

Using For loop- 

Using for loop is the naïve approach to solve this problem. We count the number of spaces between the two characters. 

def count_words(string):
    # Removing the spaces from start and end
    string1=string.strip()
    # Initializing the count from 1 because we there is no space at the last 
    count=1
    # Iterating through the string
    for i in string1:
    # If we encounter space, increment the count with 1.
        if i==" ":
            count+=1
     
    return count

string="Python is an interpreted, high-level, general-purpose programming language"
print("'{}'".format(string),"has total words:",count_words(string))
string2=" Hi. My name is Ashwini "
print("'{}'".format(string2),"has total words:",count_words(string2))
python count words in string

Output- 

'Python is an interpreted, high-level, general-purpose programming language' has total words: 8' 
Hi. My name is Ashwini ' has total words: 5 

Using split() to count words in a string

We can use split() function to count words in string. 

def word_count(string):
    # Here we are removing the spaces from start and end,
    # and breaking every word whenever we encounter a space
    # and storing them in a list. The len of the list is the
    # total count of words.
    return(len(string.strip().split(" ")))

string="Python is an interpreted, high-level, general-purpose programming language"
print("'{}'".format(string),"has total words:",count_words(string))
string2=" Hi. My name is Ashwini "
print("'{}'".format(string2),"has total words:",word_count(string2))

Output- 

'Python is an interpreted, high-level, general-purpose programming language' has total words: 8' 
Hi. My name is Ashwini ' has total words: 5 

Count frequency of words in string in python 

Using dictionary

def wordFrequency(string):
    # converting the string into lowercase
    string=string.lower()
    # Whenever we encounter a space, break the string
    string=string.split(" ")
    # Initializing a dictionary to store the frequency of words
    word_frequency={}
    # Iterating through the string
    for i in string:
    
    # If the word is already in the keys, increment its frequency
        if i in word_frequency:
            word_frequency[i]+=1
            
    # It means that this is the first occurence of the word
        else:
            word_frequency[i]=1
    return(word_frequency)

string="Woodchuck How much wood would a woodchuck chuck if a woodchuck could chuck wood ?" 
print(wordFrequency(string)) 

Output- 

{'woodchuck': 3, 'how': 1, 'much': 1, 'wood': 2, 'would': 1, 'a': 2, 'chuck': 2, 'if': 1, 'could': 1, '?': 1} 

Using Count() 

Count() can be used to count the number of times a word occurs in a string or in other words it is used to tell the frequency of a word in a string. We just need to pass the word in the argument.  

def return_count(string,word):
    string=string.lower() 
    # In string, what is the count that word occurs
    return string.count(word)

string2="Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?"
return_count(string2,'piper')

Output- 

If we want to know the number of times every word occurred, we can make a function for that. 

set1=set()
string="Woodchuck How much wood would a woodchuck chuck if a woodchuck could chuck wood ?"
string=string.lower()
# splitting the string  whenever we encounter a space
string=string.split(" ")
# iterate through list-string
for i in string:
    # Storing the word and its frequency in the form of tuple in a set
    # Set is used to avoid repetition
    set1.add((i,string.count(i)))
print(set1)

Output- 

{('how', 1), ('would', 1), ('woodchuck', 3), ('a', 2), ('chuck', 2), ('could', 1), ('if', 1), ('?', 1), ('wood', 2), ('much', 1)} 

If we want to know how many times a particular word occur in a string in an interval, we can use start and end parameters of count(). 

For example- 

string="Can you can a can as a canner can can a can?"
# if you want to take cases into account remove this line
string=string.lower()
# between index=8 and 17, how many times the word 'can' occurs
print(string.count("can",8,17))

Output- 

Must Read:

Conclusion 

In the current era, data is very important. And as the world of Data Science is growing rapidly, and that too using python, data preprocessing is very important. We need to count words in a string in python to preprocess textual data and for that, the above-discussed methods are very important. 

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Leave a Reply