.count()
Published May 5, 2021Updated Jul 29, 2023
Contribute to Docs
Searches a list for a particular item and returns the number of matching entries found.
Syntax
list.count(value)
The .count()
method has just one parameter:
value
: The string to search for. (Required)
Example
To count the number of 'pen'
within the backpack
list:
backpack = ['pencil', 'pen', 'notebook', 'textbook', 'pen', 'highlighter', 'pen']numPen = backpack.count('pen')print(numPen)
This results in the following output:
3
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.