How to

Count number of times each character appears in a string using Python

Recently, I needed to know how many occurrences the letters appear inside the specific sentence. As I was learning Python at that time, this looked like a perfect opportunity to use it to solve this problem and it is the focus of this article.

To count, how many times each character repeat in a string of text, I created the following script:

from collections import Counter
    
my_text = "Winter is coming!"
my_clean_text = my_text.replace(" ","").replace("!","")

list_of_chars = list(my_clean_text)
cnt = Counter(list_of_chars)
    
print("Original text: {}".format(my_text))
print("Cleaned text: {}".format(my_clean_text))
print("\nFrequency of characters:")
    
common = cnt.most_common()
    
for c in common:
    print(c[0]+":",c[1],"time(s)")

Let’s examine the above Python code in more detail:

  • Line 3:

    my_text = "Winter is coming!"

    Here we set the sentence, message or whatever string we want to analyze.

  • Line 4:

    my_clean_text = my_text.replace(" ","").replace("!","")

    You might want to remove any unwanted characters from the result. If you want to count the frequency of all the characters in the string, simply remove this line. In our case, we removed the white space and ! character by using str.replace function that replaces the unwanted character with an empty string.

  • Line 6:

    list_of_chars = list(my_clean_text)
  • From the text, that we just cleaned, we need to store each character as an element in the list. We will use this list in the next line. There are many ways to convert string into a list of characters, in our case, we simply use the Python list() function.

  • Line 7:

    cnt = Counter(list_of_chars)

    We then use this list of characters as an argument on a Counter object from collections module. The Counter class is used to count the unique elements in the list. This is the description of Counter in the official python documentation:

  • A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

  • Line 13:

    common = cnt.most_common()

    Finally, we use the most_common() method of the Counter, which returns a list of all elements, ordered from most common to the least common. Elements in the returned List are tuples containing the element in question and their count, for example, ('i', 3). Both values are printed on lines 15 and 16 for each unique character, ordered from most frequent to least frequent.

The above script will generate the following output:

Original text: Winter is coming!
Cleaned text: Winteriscoming

Frequency of characters:
i: 3 time(s)
n: 2 time(s)
W: 1 time(s)
t: 1 time(s)
e: 1 time(s)
r: 1 time(s)
s: 1 time(s)
c: 1 time(s)
o: 1 time(s)
m: 1 time(s)
g: 1 time(s)

Conclusion

There are many ways to count the number of times, the character occurs in a text. In this article, we achieved this with the help of the Counter class from the collections module.

If you have any other interesting solution to this problem, that you would like to share, let me know and I’ll add it here.

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.