Write a Python program to count the occurrences of a word in a given sentence

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.

2

Conclusion

If you have any doubts regarding the program, ask them in the comment section.

Write a Python program to count the occurrences of a word in a given sentence


Write a Python program to count the occurrences of a word in a given sentence

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

Input: "This is a sample Python program, Welcome to World Of Python Programming!"
Output: Number of occurrences found in the string: 2

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:
2

Source 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 Here

Data 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= len

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
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
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
0
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
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0len
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
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
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
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0len
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
9
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
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
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
0
6
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= len
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
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
0
The 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
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Output

The 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
0
6
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
0
The 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
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Output

The 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
0
6
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
08
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
09
The 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
11
The 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
0
The 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
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Output

The 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
0
6
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
36
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
37
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
38
6
4
The 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
0
The 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
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Output

The 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= len
The 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
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
64
The 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

Output

6

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.

How do you count occurrences in a sentence in Python?

One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string . count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.

How to count the number of occurrences of a word in a string in Python?

Approach:.
First, we split the string by spaces in a..
Then, take a variable count = 0 and in every true condition we increment the count by 1..
Now run a loop at 0 to length of string and check if our string is equal to the word..

How to count the occurrences of a word in a text file Python?

To count the number of occurrences of a specific word in a text file, read the content of text file to a string and use String. count() function with the word passed as argument to the count() function.

How do you count occurrences of a word in a list Python?

1) Using count() method count() is the in-built function by which python count occurrences in list. It is the easiest among all other methods used to count the occurrence. Count() methods take one argument, i.e., the element for which the number of occurrences is to be counted.