Editor settings

General settings

Save settings

Learn to program with Python

Count all letters in a string #

Write a function that takes a string as input and count the occurrence of each letter (case-insensitive) and return a dictionary with the characters as keys and their respective counts as values. Only count English alphabet and ignore everything else.


Tests #

  • count_letters("Hello World!") should return {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}
  • count_letters("Python is Fun!") should return {'p': 1, 'y': 1, 't': 1, 'h': 1, 'o': 1, 'n': 2, 'i': 1, 's': 1, 'f': 1, 'u': 1}
  • count_letters("Mississippi") should return {'m': 1, 'i': 4, 's': 4, 'p': 2}
  • count_letters("OpenAI+") should return {'o': 1, 'p': 1, 'e': 1, 'n': 1, 'a': 1, 'i': 1}
  • count_letters("Abra Kadabra") should return {'a': 5, 'b': 2, 'r': 2, 'k': 1, 'd': 1}
Output will be displayed here