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 True and 1 can both match the same search value.
  • Only string counting supports optional start and end bounds.
  • 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

value

any

The item to count in a list using equality comparison

sub

str

The substring to count in a string

start

int, optional

The start index for the string search, similar to slicing

end

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 raises list.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 as numbers.count(404). The string version, str.count() with no argument, raises count() 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).

List counting uses equality, while string counting uses non-overlapping substring matches
List counting uses equality, while string counting uses non-overlapping substring matches

Points to remember

  • list.count() and tuple.count() take exactly one argument, while str.count() can also take start and end.
  • String matching is case-sensitive, so "Error".count("error") returns 0.
  • Each list.count() call scans the whole list, so it takes O(n) time, and counting m different values this way takes O(n×m).
Question

If I need counts for many different values, should I keep calling count()?

Show Answer

A quick comparison helps separate counting from searching.

Counting and searching methods compared

Method

Best use

Key difference

list.count(x)

One value in a list

Counts equal items only

str.count(sub)

Substrings in text

Counts non-overlapping matches

str.find(sub)

First position in text

Returns an index, not a count

collections.Counter

Many counts from one dataset

Builds all frequencies in one pass

Check your understanding with these quick questions.

Quiz
1

What does list.count(404) return?

A)

The first index of 404

B)

The number of times 404 appears

C)

A list of all 404 positions

D)

True or False

Question 1 of 30 attempted

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the `count()` function?

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 does `list.count()` do?

list.count() returns how many elements in a list are equal to the value you pass. It checks each item using equality comparison.


How to count from 1 to 100 in Python?

To count from 1 to 100 in Python, use range(1, 101) in a loop. This is different from count(), which counts occurrences in a sequence.

for n in range(1, 101):
    print(n)

What is the use of `count()`?

count() is used to find how many times something appears in a sequence. For example, [404, 500, 404].count(404) returns 2.


What does `count()` do in a string?

In a string, count() returns the number of non-overlapping occurrences of a substring. It also supports optional start and end bounds, as described in string basics.


Free Resources

Copyright ©2026 Educative, Inc. All rights reserved