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
# 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
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