Given a string, count the occurrence of each word via python,

Given a string, count the occurrence of each word

tiger analytics
Input:

consider any string,

str = "India is great country"

word = "portal"

output:
1

Solve:

#make function
def countOccurrences(str, word):
 
    wordslist = list(str.split())

    return wordslist.count(word)

 
 
# our context where we find out word
str = "India is great country"
word = "great"
print(countOccurrences(str, word))


Comments