Ask Question
11 January, 06:27

Use a dictionary to count the frequency of letters in the input string. Only letters should be counted, not blank spaces, numbers, or punctuation. Upper case should be considered the same as lower case. For example, count_letters ("This is a sentence.") should return {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}.

+2
Answers (1)
  1. 11 January, 08:23
    0
    def count_letters (text):

    result = {}

    # Go through each letter in the text

    convert_text = text. lower ()

    for letter in convert_text:

    if letter. isalpha ():

    if letter in result:

    result[letter] + = 1

    else:

    result[letter] = 1

    return result

    print (count_letters ("AaBbCc"))

    # Should be {'a': 2, 'b': 2, 'c': 2}

    print (count_letters ("Math is fun! 2+2=4"))

    # Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

    print (count_letters ("This is a sentence."))

    # Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Use a dictionary to count the frequency of letters in the input string. Only letters should be counted, not blank spaces, numbers, or ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers