Quick actions

cmd+k|ctrl+k

Navigation

Languages

20240415 Word Count

Snippet info

Language

Python

Visibility

public

Author

maxine-chiu

Created

2024-04-15T04:33:26.070319Z

Updated

2024-04-15T05:01:25.445196Z

""" 10. ***Word Count***:
Write a Python function that takes a string as input and returns the count of each word in the string"""

def word_count(sentence):
    words = sentence.split() # split words into list
    word_count_dict = {}
    for word in words:
        if word in word_count_dict:
            word_count_dict[word] += 1
        else:
            word_count_dict[word] = 1
    return word_count_dict
result = word_count("the quick brown fox jumps over the lazy dog")
print(result)
INFO