python program to count the occurrences of each word in a given string sentence code example
Example 1: Python Program to Count the Occurrences of a Word in a Text File
file = open("C:\workspace\python\data.txt", "r")
data = file.read()
occurrences = data.count("python")
print('Number of occurrences of the word :', occurrences)
Example 2: how to count the occurrence of a word in string python
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count(substring)
print("The count is:", count)