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.

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.

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 ()
.

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.

Output:
Count of e in Python.Engineering is: 4
Roger Jennings is an author and consultant specializing in Microsoft .NET ntier database applications and dataintensive Windows Communication Foundation (WCF) Web services with SQL Server. He’s be...
10/07/2020
Pandas 1.x Cookbook: Practical recipes for scientific computing, time series analysis, and exploratory data analysis using Python, 2nd Edition....
05/09/2021
If you can program, you are ready to grapple with Bayesian statistics. In this book, you'll learn how to solve statistical problems using Python code instead of math formulas, using discrete probabili...
28/08/2021
Taking into account the development of modern programming, especially the emerging programming languages that reflect modern practice, Numerical Programming: A Practical Guide for Scientists and...
08/08/2021