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.
# 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
Recommended Posts:
- 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 | Frequency of each character in String
- Python program for removing i-th character from a string
- Ways to remove i'th character from string in Python
- Python | Check order of character in string using OrderedDict( )
- Generate two output strings depending upon occurrence of character in input string in Python
- Python String | count()
- Python | Count and display vowels in a string
- Python | Count all prefixes in given string with greatest frequency
- Python | Count the Number of matching characters in a pair of string
- Python program to count number of vowels using sets in given string
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Program to check if a string contains any special character
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.



