Editor

Count Each Letter

#537 · Dictionaries and sets · Beginner · 30 XP · about 5 min

Write letter_counts(text) that returns a dictionary mapping each letter in text to the number of times it appears.

  • Uppercase and lowercase count as the same letter, and the keys are lowercase.
  • Anything that is not a letter (spaces, digits, punctuation) is left out.
  • The letters go into the dictionary in the order they first appear.

Examples

letter_counts('Hello')  # returns {'h': 1, 'e': 1, 'l': 2, 'o': 1}
letter_counts('A-ha!')  # returns {'a': 2, 'h': 1}
letter_counts('123')    # returns {}

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.