Editor

Count the Character Types

#538 · Strings · Beginner · 30 XP · about 5 min

Write char_types(text) that counts five kinds of character in text and returns the counts in a dictionary with these keys, in this order:

  • 'upper': uppercase letters
  • 'lower': lowercase letters
  • 'digits': the digits 0 to 9
  • 'spaces': spaces
  • 'other': everything else, such as punctuation

Every key is in the dictionary, even when its count is 0.

Examples

char_types('Hello World 42!')
# returns {'upper': 2, 'lower': 8, 'digits': 2, 'spaces': 2, 'other': 1}
char_types('')
# returns {'upper': 0, 'lower': 0, 'digits': 0, 'spaces': 0, 'other': 0}

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.