count() returns how many times a value or substring appears and gives back an integer. In Python, it is commonly used as a method on sequences such as lists and strings.
What is the Python count()?
Python count() returns how many times a value or substring appears in a sequence. Use list.count(x) for lists and str.count(sub[, start[, end]]) for strings, but use collections.Counter when you need many counts from the same data.
For the string "aaaa", "aaaa".count("aa") returns 2, not 3, because Python counts only non-overlapping matches.
Key takeaways:
- Lists and tuples count items by equality, so
Trueand1can both match the same search value.- Only string counting supports optional
startandendbounds.- One count on a list is a full scan, so repeated queries are better handled with
Counter.
What is Python count()?
The count() method on Python’s built-in sequences returns an integer occurrence count. For lists and tuples, list.count(value) and tuple.count(value) count items equal to value; for strings, str.count(sub[, start[, end]]) counts non-overlapping substring matches.
Developers use this method for quick one-off counts, such as checking repeated status codes in a list or repeated words in a message.
Syntax
list.count(value)
tuple.count(value)
str.count(sub[, start[, end]])
Parameters
These methods take different parameters depending on the type.
Parameters for list and string counting methods
Parameter | Type | Meaning |
| any | The item to count in a list using equality comparison |
| str | The substring to count in a string |
| int, optional | The start index for the string search, similar to slicing |
| int, optional | The stop index for the string search |
Return value
count() returns an int. 0 means no match was found, and any positive value is the number of matches. For strings, str.count("") is a special case: it returns len(s) + 1, as documented in the Python string method reference.
Note: Calling
list.count()with no value raiseslist.count() takes exactly one argument (0 given)because the list method requires the item to search for. Fix it by passing the target value, such asnumbers.count(404). The string version,str.count()with no argument, raisescount() takes at least 1 argument (0 given).
Example
This program shows list counting, string counting, a bounded string search, and two edge cases.
errors = [404, 500, 404, True, 1, 404]log = "error 404, error 500, error 404"print("404 in errors:", errors.count(404))print("1 in errors:", errors.count(1))print("True in errors:", errors.count(True))print("'error' in log:", log.count("error"))print("'404' in log:", log.count("404"))print("'404' from index 10:", log.count("404", 10))text = "aaaa"print("'aa' in 'aaaa':", text.count("aa"))print("empty substring in 'aaaa':", text.count(""))
Line 1: This list includes repeated integers and the True/1 equality edge case.
Line 2: This string contains repeated text for substring counting.
Lines 4-6: list.count() scans the whole list and counts equal items. Because True == 1, both searches count those two elements.
Lines 8-10: str.count() counts matching substrings, and the last call starts searching at index 10.
Lines 12-14: These lines show non-overlapping matching and the empty-substring rule.
Three results surprise most readers: True in errors: 2 (it also counts the 1), 'aa' in 'aaaa': 2 (matches never overlap), and empty substring in 'aaaa': 5 (the empty string matches at every position, including both ends).
Points to remember
list.count()andtuple.count()take exactly one argument, whilestr.count()can also takestartandend.- String matching is case-sensitive, so
"Error".count("error")returns0. - Each
list.count()call scans the whole list, so it takesO(n)time, and countingmdifferent values this way takesO(n×m).
If I need counts for many different values, should I keep calling count()?
A quick comparison helps separate counting from searching.
Counting and searching methods compared
Method | Best use | Key difference |
| One value in a list | Counts equal items only |
| Substrings in text | Counts non-overlapping matches |
| First position in text | Returns an index, not a count |
| Many counts from one dataset | Builds all frequencies in one pass |
Check your understanding with these quick questions.
What does list.count(404) return?
The first index of 404
The number of times 404 appears
A list of all 404 positions
True or False
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is the `count()` function?
What does `list.count()` do?
How to count from 1 to 100 in Python?
What is the use of `count()`?
What does `count()` do in a string?
Free Resources