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.
- Normalizing text and extracting words with re.findall
- Counting with collections.Counter
- Finding the most common words
"python"2"is"2"clear"1"practical"1Text → 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.
text.lower()🔡 Read every ballot the same wayre.findall(r"[a-z]+", ...)✂️ Cut out the names, drop the punctuationCounter(words)📊 The tally sheet: name and number of marksprint(Counter(words))🏆 The results, most marks firstWord 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
text.lower()makes"Python"and"python"the same word.re.findall(r"[a-z]+", ...)returns every run of letters, so periods and spaces disappear.Counter(words)counts each word and shows the most common first.- 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
Counterclass.
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
from collections import Counter
import re
text = "Python is clear. Python is practical."
words = re.findall(r"[a-z]+", text.lower())
print(Counter(words))
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Forgetting to lowercase
"Python" and "python" are counted as two different words.
re.findall(r"[a-z]+", text)
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.
- Count Words Count the words in a sentence. Easy
- Character Frequency Count how often each character appears. Easy
- Tournament Standings Rank teams from match records. Medium