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


Topic: Write a Python program to count the occurrences of each word in a given sentence.

Solution

def word_count(str):
    counts = dict()
    words = str.split()
    for word in words:
        if word in counts:
            counts[word] += 1
        else:
            counts[word] = 1
    return counts



List all Python Programs