Skip to content

7. Count Words in Reviews

Difficulty: Easy · Topic: Core Python & data structures

Write count_words(text): return a dict from each word in text to how many times it appears.

Example: "Great food. Great price!" gives {'great': 2, 'food': 1, 'price': 1}.

Your code runs in CPython on our servers against 6 tests, including hidden ones.

Sample test

count_words("Great food. Great price!")

Expected: {'great': 2, 'food': 1, 'price': 1}

Hints

Hint 1text.split() with no argument splits on any run of whitespace and never returns empty strings.
Hint 2strip with a string of characters removes any of them from both ends of the word, and only from the ends.
Hint 3counts[w] = counts.get(w, 0) + 1, or counts.setdefault(w, 0) first, adds to a running count.

Learn the concepts

More Python problems

All Python problems