Count number of occurrences of a character in string

Python : Count number of occurrences of a character in string

This tutorial guides you on how to count number of occurrences of a character in a given string. This logic would be very useful when you wanted to remove duplicates or checking unwanted characters from the string in Python programming.

Python : Count number of occurrences of a character in string

Count number of occurrences of a character in string

There are numerous options available in Python programming to count the number of occurrences of a character in string. In the first section we are going to see how to use String count(). 

Python String count()

The count() method of Python string returns the number of occurrences of a substring in the given string. Let’s say you wanted to find the number of times the character ‘s’ repeated in the string “sneppets“. This is easy method when compared to other approaches, hence it is quite popular.

For example,

mystr = "sneppets"
mystr.count('s')

Output

2

Using Counter Class of collections library

This is less commonly used approach compared to the String count() method. But you can use Counter class of collections library which also performs similar logic as in the above approach.

For example, to count the number of occurrence of character “s” in string “sneppets” using Counter class.

from collections import Counter
mystr = 'sneppets'
c = Counter(mystr)
c['s']

Output

2

Using for loop and count

This is native approach which uses for loop and the counter. Here we need to iterate the given string and check each single character in the string for a particular character. If the character is found then increase the counter. The value of the count is the number of occurrence of that particular character.

For example,

mystr = "sneppets"
count = 0
for element in mystr:
    if (element == 's'):
        count = count + 1
count

Output

2

Number of occurrences of all characters in a given string

This approach will give you an output which prints all the unique characters and the number of occurrences of each character at one shot.

For example,

def getcount(str):
    countval = {}
    for element in set(str):
       countval[element] = str.count(element)
    return countval

Let’s pass input string to getcount() method.

getcount("sneppets")

Output

The output prints the number of times each character repeated in the given string.

{'p': 2, 't': 1, 'n': 1, 'e': 2, 's': 2}

Using List Comprehension

You can also use list comprehension in the above getcount() method which will give you the similar output.

def getcount(str):
    return {element: str.count(element) for element in set(str)}

Output

getcount("sneppets")
{'p': 2, 't': 1, 'n': 1, 'e': 2, 's': 2}

That’s it. You had learnt various approaches for finding the count of characters that are repeated in the given string.

Hope it is helpful 🙂

You’ll also like:

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments