Beginner Python Exercises

Count Word Frequency in Python with Counter

Which words appear most? Split the text, count with collections.Counter and get the top words in one call.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Normalizing text and extracting words with re.findall
  • Counting with collections.Counter
  • Finding the most common words
"Python is clear. Python is practical."
"python"2
"is"2
"clear"1
"practical"1

Text → words → counts

The problem

Which words appear most in customer reviews, in a book or in search queries? Counting by hand is impossible for real texts, and "Python", "python" and "python." must count as the same word.

Think of it like this

Counting word frequency is like sorting ballots after a vote. You first make every ballot readable the same way (lowercase, no punctuation), then you put a mark next to each name every time it appears. The tally sheet shows the winner.

From counting votes to code
text.lower()🔡 Read every ballot the same way
re.findall(r"[a-z]+", ...)✂️ Cut out the names, drop the punctuation
Counter(words)📊 The tally sheet: name and number of marks
print(Counter(words))🏆 The results, most marks first
Normalize, split into words, then count.
In one sentence

Word frequency is how many times each distinct word appears in a text, usually after making the words comparable by lowercasing them and removing punctuation.

Key points

  1. text.lower() makes "Python" and "python" the same word.
  2. re.findall(r"[a-z]+", ...) returns every run of letters, so periods and spaces disappear.
  3. Counter(words) counts each word and shows the most common first.
  4. Both "python" and "is" appear twice in the example.

When should you use it?

  • Finding common words in reviews, logs or survey answers.
  • A first step in text analysis and search.
  • Practicing text cleaning and the Counter class.

When not to use it

  • Texts with accented letters such as French or Vietnamese: [a-z]+ drops those characters, so use a Unicode-aware pattern.
  • Real language analysis: you also need to merge forms such as run and running, which needs an NLP library.

Example code

Run code →
main.py
from collections import Counter
import re

text = "Python is clear. Python is practical."
words = re.findall(r"[a-z]+", text.lower())

print(Counter(words))
Get a Run button for this example

Result of running the code

Counter({'python': 2, 'is': 2, 'clear': 1, 'practical': 1})

Code explanation

text.lower() turns every letter into lowercase so that capitalized and normal words match. re.findall with the pattern [a-z]+ then returns each run of letters as a list item, which drops the spaces and periods. Counter goes through that list once and records how many times each word appears, printing the most frequent first.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Forgetting to lowercase

"Python" and "python" are counted as two different words.

Problem
re.findall(r"[a-z]+", text)
Fix
re.findall(r"[a-z]+", text.lower())

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges