Beginner Python Exercises

Count Character Frequency in Python

How many times does each letter appear? Build a frequency dictionary, or let Counter do it.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Counting with a dictionary and .get()
  • collections.Counter
  • Printing counts in order
"banana"
'a'3
'b'1
'n'2

One 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.

From a tally board to code
Counter("banana")📋 Walk through the word and tally each letter
frequencies📊 The board: a 3, n 2, b 1
sorted(frequencies)🔤 Read the columns in alphabetical order
frequencies[character]🔎 How many marks in one column
One pass through the text gives every count.
In one sentence

A character frequency table maps each distinct character to the number of times it appears in a text.

Key points

  1. Counter("banana") counts every character in one pass.
  2. frequencies[character] reads the count for one character, like a dictionary.
  3. sorted(frequencies) gives the characters in alphabetical order for stable output.
  4. 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

Run code →
main.py
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]}")
Get a Run button for this example

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.

  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

Reading a missing key from a normal dictionary

A plain dict raises KeyError for an unseen character; Counter returns 0.

Problem
counts = {}
counts["z"] += 1  # KeyError
Fix
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.

All challenges