- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
Python program to count occurrences of a word in a string
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.
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.
## 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)
If you run the above program, you will get the following results.
If you have any doubts regarding the program, ask them in the comment section.
- Related Articles
- Write a python program to count occurrences of a word in string?
- Java program to count occurrences of a word in string
- C# program to count occurrences of a word in string
- Count occurrences of a character in string in Python
- Python program to count occurrences of an element in a tuple
- How to replace all occurrences of a word in a string with another word in java?
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- How to Count Word Occurrences in a Text File using Shell Script?
- Count occurrences of a character in a repeated string in C++
- Count occurrences of an element in a list in Python
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- Count of occurrences of a “1(0+)1” pattern in a string in C++
- Python program to Count words in a given string?