# Python countof() | Operator Method

The Python countof() method is used to return the number of elements in `a` that is equal to `b`. The method returns the number of elements in `a` that is equal to `b`.

``operator.countOf(a, b)``
• `a`: This is a list or a string.
• `b`: This is the element we are interested in finding in `a`

## Python countof() method

Simple example code import the `operator` module. Define `a` and `b` and obtain the count of `b` in `a` using the `countOf()` method and print the result.

``````import operator

a = "Hello World"
b = "e"

print("'%s', '%s' = %s" % (a, b, operator.countOf(a, b)))

a = [1, 2, 3, 4]
b = 5

print("'%s', '%s' = %s" % (a, b, operator.countOf(a, b)))``````

Output:

Count the number of occurrences of a given value in a dictionary.

``````from operator import *

d = {"score": 3, "score1": 1, "score2": 3, "score3": 1, "score4": 3}

print("3 in dict =", countOf(d.values(), 3))
print("1 in dict =", countOf(d.values(), 1))
print("4 in dict =", countOf(d.values(), 4))``````

Output:

3 in dict = 3
1 in dict = 2
4 in dict = 0

Count the number of occurrences of a given value in a tuple.

``````from operator import *

tup = (1, 2, 3, 3, 3)
tup1 = ('a', 'b', 'c', 'b', 'd')

print("b in tuple1 =", countOf(tup1, "b"))
print("e in tuple1 =", countOf(tup1, "e"))
print("3 in tuple =", countOf(tup, 3))
``````

Output:

b in tuple1 = 2
e in tuple1 = 0
3 in tuple = 3

Do comment if you have any doubts or suggestions on this Python operator module method topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.