count method of tuple in Python

The count method returns the number of occurrences of a specific value in a Python tuple object. The Python example returns the number of times a token occurs in the famous Negative-positive restatement from John F. Kennedy.

Tuple Updated 18 Jul 2021 1 min read

Method Name

count

Method Signature

count()

Parameters and return value

None

Overview

  • The count() method of tuple returns the number of times an element with a specific value occurs in a Python tuple.
  • The len() function is different. It provides the total number of elements present in the tuple.

Example

python
# Example Python program that creates

            # a tuple with duplicate elements and

            # returns the count of elements with specific value

tokens = ("Ask", "not",  "what",  "your", "country", "can",

                      "do", "for", "you", ";", "ask", "what", "you", "can",

                      "do", "for", "your", "country")

            print("Total number of tokens:")

            print(len(tokens))

token = "country";

            print("The token '%s' occured %d number of times in the tuple"%(token, tokens.count(token)))

token = "you";

            print("The token '%s' occured %d number of times in the tuple"%(token, tokens.count(token)))

Output

Output
Total number of tokens:

18

The token 'country' occured 2 number of times in the tuple

The token 'you' occured 2 number of times in the tuple