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.
# 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)) |
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.
# 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)) |
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.
# 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'])) |
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().
# 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)) |
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.
# 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)) |
Output :
Count of e in GeeksforGeeks is : 4
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Deleting all occurrences of character
- Python - Non-Overlapping occurrences of N Repeated K character
- Python | Count String occurrences in mixed list
- Count occurrences of a word in string
- Python | Count occurrences of an element in a list
- Python | Count occurrences of an element in a Tuple
- Python | Count occurrences of each word in given text file (Using dictionary)
- Python program to Count the Number of occurrences of a key-value pair in a text file
- Python | All occurrences of substring in string
- Python - Characters Index occurrences in String
- Python - Replace all occurrences of a substring in a string
- Python | Insert character after every character pair
- Python program to read character by character from a file
- Python | Remove all occurrences in nested list
- Python | Get the starting index for all occurrences of given substring
- Python | Record Similar tuple occurrences
- Python - Extract elements with Range consecutive occurrences
- Python - Replace all repeated occurrences with N
- Python | Replace characters after K occurrences
- Python - Distance between occurrences
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.

