Python | Count occurrences of a character in string

Given a string, the task is to count the frequency of a single character in that string. This particular operation on string is quite useful in many applications such as removing duplicates or detecting unwanted characters.

Method #1 : Naive method

Iterate the entire string for that particular character and then increase the counter when we encounter the particular character.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# occurrence frequency using 
# naive method 
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using naive method to get count 
# counting e 
count = 0
  
for i in test_str:
    if i == 'e':
        count = count + 1
  
# printing result 
print ("Count of e in GeeksforGeeks is : "
                            +  str(count))

chevron_right


Output :

Count of e in GeeksforGeeks is : 4

 

Method #2 : Using count()

Using count() is the most conventional method in Python to get the occurrence of any element in any container. This is easy to code and remember and hence quite popular.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# occurrence frequency using 
# count()
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using count() to get count 
# counting e 
counter = test_str.count('e')
  
# printing result 
print ("Count of e in GeeksforGeeks is : "
                           +  str(counter))

chevron_right


Output :

Count of e in GeeksforGeeks is : 4

 

Method #3 : Using collections.Counter()

This is the lesser known method to get the occurrence of the element across any container in Python. This also performs the task similar to above two method, just is a function of a different library i.e collections.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# occurrence frequency using 
# collections.Counter()
from collections import Counter
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using collections.Counter() to get count 
# counting e 
count = Counter(test_str)
  
# printing result 
print ("Count of e in GeeksforGeeks is : "
                       +  str(count['e']))

chevron_right


Output :

Count of e in GeeksforGeeks is : 4

 

Method #4 : Using lambda + sum() + map()

Lambda functions, along with sum() and map() can achieve this particular task of counting the total occurrences of particular element in a string. This uses sum() to sum up all the occurrences obtained using map().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# occurrence frequency using 
# lambda + sum() + map()
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using lambda + sum() + map() to get count 
# counting e 
count = sum(map(lambda x : 1 if 'e' in x else 0, test_str))
  
# printing result 
print ("Count of e in GeeksforGeeks is : "
                            +  str(count))

chevron_right


Output :

Count of e in GeeksforGeeks is : 4

 
Method #5 : Using re + findall()

Regular Expressions can help us to achieve many coding tasks related to strings. They can also facilitate us in achieving the task of finding the occurrence of element in string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# occurrence frequency using 
# re + findall()
import re
  
# initializing string 
test_str = "GeeksforGeeks"
  
# using re + findall() to get count 
# counting e 
count = len(re.findall("e", test_str))
  
# printing result 
print ("Count of e in GeeksforGeeks is : "
                            +  str(count))

chevron_right


Output :

Count of e in GeeksforGeeks is : 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.