Post

Python Tuple count() Method

The count() method returns the number of times a specified value appears in a tuple.

Python Tuple count() Method

The count() method is used to count how many times a specific element appears in a tuple.

The syntax of the count() method is:

1
tuple.count(value)

count() Parameters

The count() method takes only one parameter as argument.

  • value - the element to be counted in the tuple

Example 1: How to use count() method in python?

1
2
3
4
5
6
7
8
9
10
11
12
13
# create a tuple
numbers = (1, 2, 3, 2, 5, 2, 6)

# count number of times 2 appears
count = numbers.count(2)
print(count)

# create a tuple of strings
fruits = ('apple', 'banana', 'apple', 'cherry')

# count number of times 'apple' appears
count = fruits.count('apple')
print(count)

Output:

1
2
3
2

Rules of count()

  • It returns 0 if the value is not found in the tuple
  • The count is case-sensitive for strings
  • It returns the exact count of occurrences of the specified value
Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.