Python .count()
Published Oct 14, 2022
Contribute to Docs
The .count() method returns the number of occurrences of a specific value in a tuple.
Syntax
tuple_instance.count(value)
The value parameter is required. If the value does not exist in the tuple_instance, 0 is returned. The .count() method is type-sensitive (e.g., string representations of numbers and actual numbers are not counted in the returned total).
Example
The following example showcases the .count() method:
my_tuple = (2, 4, "6", "2", 8, "8", "2")print(my_tuple.count(2))# Output: 1
Since the .count() method is type-sensitive, the snippet above returned 1 (for one occurrence of the number 2, excluding the string representation of "2").
Codebyte Example
In the following example, the .count() method returns 0 because the value being counted doesn’t exist in my_tuple:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours