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. Show 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. Algorithm1. 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) OutputIf you run the above program, you will get the following results. 2 ConclusionIf 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. 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 Codestr = "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
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 62 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 65 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 66
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 67 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 len The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 64 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 len The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 69 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 61 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62 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. Python3The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 63 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 = The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61
60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 63 64 65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64 68 = len The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 61
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65 64 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68 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 Python3The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 63 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 = # Quick Two Line Codes 7
60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 63 64 65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64 68 = len countOfWords 9= 0= 1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65 64 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68 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. Python3The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 63 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 = # Quick Two Line Codes 7
60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 63 64 65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64 68 = The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 608 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 609 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 610 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 611 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 612 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 613
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65 64 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68 Output The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 6 Method #4: Using count() method Python3The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 63 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 = The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61
60 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 63 64 65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64 68 = The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 636 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 637 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 638 64 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 640
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65 64 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68 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
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 649
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 650 The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67 = The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 654 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 655 = The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 657
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 658 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 659 = len The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 662 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 664 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 665 The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 666 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.
|