Count Character Frequency in Python
How many times does each letter appear? Build a frequency dictionary, or let Counter do it.
- Counting with a dictionary and .get()
- collections.Counter
- Printing counts in order
'a'3'b'1'n'2One counter per character
The problem
How often does each letter appear? Frequency tables are used in compression, simple ciphers, typing statistics and data cleaning. Keeping a separate counter variable for every possible character is impossible.
Think of it like this
A frequency table is like a tally board with one column per letter. You walk through the word once, and each time you see a letter you add a mark in its column. Counter builds the whole board for you.
Counter("banana")📋 Walk through the word and tally each letterfrequencies📊 The board: a 3, n 2, b 1sorted(frequencies)🔤 Read the columns in alphabetical orderfrequencies[character]🔎 How many marks in one columnA character frequency table maps each distinct character to the number of times it appears in a text.
Key points
Counter("banana")counts every character in one pass.frequencies[character]reads the count for one character, like a dictionary.sorted(frequencies)gives the characters in alphabetical order for stable output.- The result shows a=3, b=1, n=2.
When should you use it?
- Letter statistics for ciphers and puzzles.
- Finding repeated or missing characters in data.
- Checking anagrams and permutations.
When not to use it
- Counting only one character:
text.count("a")is simpler. - Counting words instead of characters: split the text into words first.
Example code
from collections import Counter
# Counter maps each character to its occurrence count.
frequencies = Counter("banana")
for character in sorted(frequencies):
print(f"{character}={frequencies[character]}")
Result of running the code
a=3
b=1
n=2
Code explanation
Counter("banana") goes through the string once and records how many times each character appears. Sorting frequencies gives the characters in alphabetical order, so the output is the same on every run. Inside the loop, frequencies[character] reads each count just like a dictionary lookup.
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
Reading a missing key from a normal dictionary
A plain dict raises KeyError for an unseen character; Counter returns 0.
counts = {}
counts["z"] += 1 # KeyError
counts = {}
counts["z"] = counts.get("z", 0) + 1
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.
- Character Frequency Count how often each character appears. Easy
- First Unique Character Find the first character that appears only once. Easy