DEV Community

Lucretius Biah
Lucretius Biah

Posted on • Originally published at kudadam.com on

Python: How To Make A Letter Counter

Table Of Contents

  1. The code
  2. Testing the fuction
  3. Modified Code

So this blog post is about a problem which I found in Sololearn where you are to make a simple program that takes a group of text and then returns a dictionary showing the occurence of each letter

So a letter counter is simply a program which takes a text or string and returns the number of occurrence of each letter. We are going to make a python function which takes a string and returns a dictionary with the number of occurence of each letter

The code

def letter_counter(text):
    dict = {}
    for i in text:
        dict[i] = text.count(i)
    return dict
Enter fullscreen mode Exit fullscreen mode

So a function called letter_counter was created and it takes text as parameter. Then inside the function, we create an empty dictionary called dict. Then on the next line, we iterate through all the characters in the text variable, we then include the current letter inside the dict dictionary. Then we use the .count method of the string class to find the number of occurrence of the current letter and assign it as the value of the letter in the dictionary. Then finally, we return the dict variable.

Testing the fuction

So let’s text the fuction we just created with this string “Lucretius is a great developer”.

text = "Lucretius is a great developer"
print(letter_counter(text))
Enter fullscreen mode Exit fullscreen mode
{'L': 1, 'u': 2, 'c': 1, 'r': 3, 'e': 5, 't': 2, 'i': 2, 's': 2, ' ': 4, 'a': 2, 'g': 1, 'd': 1, 'v': 1, 'l': 1, 'o': 1, 'p': 1}

[Program finished]
Enter fullscreen mode Exit fullscreen mode

Well, the fuction works alright but there is a slight problem.

If you are to check the output carefully, you can see the fuction found the number of occurrence for whitespaces as well but whitespaces are not letters so let’s modify our fuction to exclude whitespaces.

Modified Code

from string import ascii_letters
def letter_counter(text):
    dict = {}
    for i in text:
        if i in ascii_letters:
            dict[i] = text.count(i)
    return dict
Enter fullscreen mode Exit fullscreen mode

Now if you are to run the fuction, you will realize that it excludes all non-letters from the dictionary.

What we did was to import the ascii_letters constant from the string module, then before adding a character to the dict variable, we check if the current character is included in the letters constant, if it is, we add it; meaning it’s a letter, else, we exclude it.

Happy Coding 😄

Top comments (0)