In this tutorial, we are going to write a program that counts the number of times a word occurs in the string. You are given the word and a string, we have to calculate the frequency of the word in the string.
Suppose we have a string I am a programmer. I am a student. And the word is. The program that we are going to write will return a number 2 as the word occurs two times in the string.
Let's follow the below steps to achieve our goal.
Algorithm
1. Initialize the string and the word as two variables. 2. Split the string at spaces using the split() method. We will get a list of words. 3. Initialize a variable count to zero. 4. Iterate over the list. 4.1. Check whether the word in the list is equal to the given the word or not. 4.1.1. Increment the count if the two words are matched. 5. Print the count.Try to write the code for the program on your own first. Let's see the code.
Example
## initializing the string and the word string = "I am programmer. I am student." word = "am" ## splitting the string at space words = string.split() ## initializing count variable to 0 count = 0 ## iterating over the list for w in words: ## checking the match of the words if w == word: ## incrementint count on match count += 1 ## printing the count print(count)Output
If you run the above program, you will get the following results.
2Conclusion
If you have any doubts regarding the program, ask them in the comment section.
In this post, we will see how to count the occurrences of each word in a given string sentence.
Algorithm
Step 1: Declare a String and store it in a variable.
Step 2: Declare a variable wordCount and initialise it to 0.
Step 3: Split the string using split(“ ”) function with space as the parameter and store all the words in a list.
Step 4: Use a for loop to iterate over the words in the list. While iterating check if the word in the list matches the word given by the user and if match found then increment the value of wordCount variable by 1.
Step 5: After completing the for loop print the value of wordCount variable.
Step 6: End.
Example
Program
string="This is a sample Python program, Welcome to World Of Python Programming!" word="Python" list=[] wordCount=0 list=string.split(" ") for i in range(0,len(list)): if(word==list[i]): wordCount=wordCount+1 print("Number of occurrences found in the string:") print(wordCount)Output
Number of occurrences found in the string: 2Source Code
str = "To change the overall look your document. To change the look available in the gallery" c = dict() txt = str.split(" ") for t in txt: if t in c: c[t] += 1 else: c[t] = 1 print(c)Output
{'To': 2, 'change': 2, 'the': 3, 'overall': 1, 'look': 2, 'your': 1, 'document.': 1, 'available': 1, 'in': 1, 'gallery': 1} To download raw file Click HereData preprocessing is an important task in text classification. With the emergence of Python in the field of data science, it is essential to have certain shorthands to have the upper hand among others. This article discusses ways to count words in a sentence, it starts with space-separated words but also includes ways to in presence of special characters as well. Let’s discuss certain ways to perform this.
Quick Ninja Methods: One line Code to find count words in a sentence with Static and Dynamic Inputs.
Python3
# Quick Two Line Codes
countOfWords= lenThe original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 1The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 2
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 5The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 6
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 7
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0lenThe original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 1The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 4
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0lenThe original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 9The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 1The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 2
Output:
Method #1: Using split() split function is quite useful and usually quite generic method to get words out of the list, but this approach fails once we introduce special characters in the list.
Python3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 5
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 6
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7= The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 1
6 0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 06 3 6 4 6 5
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 5
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
6 8= lenThe original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 2
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 5 6 4 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 8
OutputThe original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6
Method #2 : Using regex(findall()) Regular expressions have to be used in case we require to handle the cases of punctuation marks or special characters in the string. This is the most elegant way in which this task can be performed.
Example
Python3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
# Quick Two Line Codes1
# Quick Two Line Codes2 # Quick Two Line Codes3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 6
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7= # Quick Two Line Codes7
6 0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 06 3 6 4 6 5
# Quick Two Line Codes1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
6 8= lencountOfWords9=0=1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 2
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 5 6 4 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 8
OutputThe original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6
Method #3 : Using sum() + strip() + split() This method performs this particular task without using regex. In this method we first check all the words consisting of all the alphabets, if so they are added to sum and then returned.
Python3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
len1
# Quick Two Line Codes2 len3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 6
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7= # Quick Two Line Codes7
6 0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 06 3 6 4 6 5
len1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
6 8= The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 08The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 09The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 10 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 11The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 12 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 13
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 2
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 5 6 4 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 8
OutputThe original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6
Method #4: Using count() method
Python3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 6
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7= The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 1
6 0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 06 3 6 4 6 5
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 4
6 8= The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 36The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 37The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 386 4The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 40
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 2
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 0The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 5 6 4 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 8
OutputThe original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6
Method #5 : Using the shlex module:
Here is a new approach using the split() method in shlex module:
Python3
# Quick Two Line Codes2 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 49
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 50
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 6 7= The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 1
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 54
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 55= The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 57
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 58
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 59= lenThe original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 62
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 3The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 64The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 65
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 66
Output6
The shlex module provides a lexical analyzer for simple shell-like syntaxes. It can be used to split a string into a list of words while taking into account quotes, escapes, and other special characters. This makes it a good choice for counting words in a sentence that may contain such characters.
Note: The shlex.split function returns a list of words, so you can use the len function to count the number of words in the list. The count method can also be used on the list to achieve the same result.