Python | Counting occurences of a character in a string

Counters | Python Methods and Functions | Regular Expressions | String Variables

Method # 1: The Naive Method

Iterate the entire string for that particular character, then increment the counter when we encounter that particular character.

# Python3 demo code
# frequency of occurrence using
# naive method

  
# initializing string

test_str = "GeeksforGeeks"

 
# using a naive counting method
# counting e

count = 0

  

for i in test_str:

if i = = 'e' :

count = count + 1

 
# print result

print ( "Count of e in Python.Engineering is:"

+   str ( count))

 

Output:

 Count of e in Python.Engineering is: 4 

Method # 2: Using count()

Using count () — the most common method in Python to get the occurrence of any item in any container. It is easy to code and remember and therefore quite popular.

# Python3 demo code
# frequency of occurrence using
# count ()

 
# initializing string

test_str = "GeeksforGeeks"

 
# using count () to get the score
# counting e

counter = test_str.count ( 'e' )

  
# print result

print ( "Count of e in Python.Engineering is:"

+   str (counter))

Output:

 Count of e in Python.Engineering is: 4 

Method # 3: Using collections.Counter()

This is a lesser known method to get the entry of an element into any container in Python. This also performs a task similar to the above two methods, it is just a function of another library i.e. collections.

# Python3 demo code
# frequency of occurrence using
# collection.Counter ()

from collections import Counter

  
# initializing string

test_str = "GeeksforGeeks"

 
# using collection.Counter () to count
# counting e

count = Counter (test_str)

  
# print result

print ( "Count of e in Python.Engineering is:"

+   str (count [ ' e' ]))

Output:

 Count of e in Python.Engineering is: 4 

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

Lambda functions with sum () and map () can solve this particular problem of counting the total number of occurrences of a certain element in a string. It uses sum () to sum all occurrences from map () .

# Python3 demo code
# frequency of occurrence using
# lambda + sum () + map ( )

 
# initialization string

test_str = "GeeksforGeeks"

 
# using lambda + sum () + map () to count
# counting e

count = sum ( map ( lambda x: 1 if 'e' in x else 0 , test_str))

 
# print result

print ( "Count of e in Python.Engineering is:"

+   str (count))

Output:

 Count of e in Python.Engineering is: 4 

Method # 5: Using re + findall ()

Regular Expressions can help us solve many problems of coding ania associated with strings. They can also help us achieve the task of finding the occurrence of an element in a string.

# Python3 demo code
# frequency of occurrence using
# re + findall ()

import re

 
# initialization string

test_str = "GeeksforGeeks"

 
# using re + findall () to count
# counting e

count = len (re.findall ( "e" , test_str))

 
# print result

print ( "Count of e in Python.Engineering is:"

+   str (count))

Output:

 Count of e in Python.Engineering is: 4 




Get Solution for free from DataCamp guru