SolveWithPython

Challenge 4: Count Character Frequency

You’ll build a frequency dictionary from a string.
This mirrors real parsing and analytics tasks.
Estimated time: 15 minutes | Difficulty: Beginner

Problem

Count how often each character appears in a string.

Inputs

  • text: str

Output

Return a dictionary of character counts.

Example

Input

Python Challenge 4 Input Count Character Frequency

Output

Python Challenge 4 Output Count Character Frequency

Constraints

  • Do not use external libraries
  • Input may be empty
  • Count all characters exactly as they appear (including spaces and punctuation)
  • Case-sensitive: "A" and "a" are different characters

Requirements

  • Function name: char_frequency(text: str) -> dict
  • Parameter: text (string)
  • Return type: dict[str, int]

Edge Cases to Consider

  • Empty string
  • Repeated characters
  • Spaces and punctuation
  • Mixed case letters

Python Challenge 4 Solution Count Character Frequency

Explanation

  • Iterate over each character and increment a counter in a dictionary
  • Time complexity: O(n)
  • Space complexity: O(k) where k is the number of unique characters

Python Challenge 4 Test Count Character Frequency

You can use the following tests above to verify your solution locally.

đź”— View reference solution on GitHub
(After you’ve tried the challenge)

Nice work!

You’ve completed this challenge. The best way to improve your Python skills is to keep practicing while the concept is fresh.

👉 Next Challenge → Filter Even Numbers

Want more practical Python challenges?
Subscribe to the Solve With Python newsletter and get new problems delivered to your inbox.