Skip to content
Exercise · Dicts and sets · lesson 10 of 11

Exercise: word frequency

Write a function word_count(text) that returns a dict: the keys are the words of the string text in lower case, and the values are how many times each word occurs.

  • word_count("the cat the") → {'the': 2, 'cat': 1}
  • word_count("Go go GO") → {'go': 3}
  • word_count("") → {}

Constraints: words consist of Latin letters, with no punctuation; there may be several spaces between words, as well as at the start and the end of the string; words that differ only in letter case count as one word.

Do not add extra imports unless the task says so explicitly.

word_count.py — python
python · 3 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way