.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

All contributors

Looking to contribute?

Learn Python on Codecademy