To count occurrences of a substring in Python, call str.count(sub). It returns how many times sub appears in the string, scanning from left to right and counting non-overlapping matches only. The Python str.count method is the fastest and simplest option for plain text; use a regular expression or a small loop when matches may overlap or you need pattern matching.
The examples below run on Python 3.13 and 3.14 and need no third-party packages. You will see the basic call, the start and end arguments, case-insensitive counting, overlapping matches, and counting words or characters across a whole text.
str.count(sub[, start[, end]]) returns an integer. The match is case-sensitive.
Note that count("the") matches the letters anywhere, including inside longer words. In "other the" the first the inside other is counted too. If you need whole words, see the word counting section below.
The argument must be a string. Passing an integer raises TypeError, so convert first with str(n) when counting digits.
Like slices, count accepts optional start and end positions. Only string[start:end] is searched.
No copy of the string is created, which matters when you search a large text repeatedly.
count never reuses characters from a previous match. After finding "aa" in "aaaa" it continues after that match, so it finds two matches, not three.
To count overlapping matches, check each starting position with startswith:
A regular expression with a lookahead does the same thing:
The lookahead matches a position without consuming characters, so the next search can start one character later. Use re.escape(sub) inside the pattern if the substring may contain special characters.
count has no ignore-case option. Normalize both the text and the substring first. Prefer casefold() over lower() for text in many languages.
Alternatively, use re.findall with the re.IGNORECASE flag when you also need patterns:
For whole words, split the text and count the tokens. collections.Counter counts every word in one pass.
list.count() compares whole items, so "other" is not counted as "the". Real text has punctuation, so strip it or use re.findall(r"\w+", text.lower()) before counting.
Other common cases:
"abc".count("") returns 4, which is len(s) + 1, because the empty string matches at every position including the end. Reject empty input if that makes no sense for your code.
A few other points:
count returns 0 for no match; it never raises an error for missing text.sub in text, which can stop at the first match.count once per substring, or use Counter when the items are tokens.bytes: b"abcabc".count(b"bc").Use text.count("a"). A single character is just a substring of length one.
Yes. "Apple".count("a") returns 0. Lowercase or casefold both sides first.
Loop over every start index with text.startswith(sub, i), or use re.findall(r"(?=sub)", text). The built-in count cannot do it.
Split the text into words with split() or a regular expression, then use list.count() or collections.Counter.
str.count(sub) counts non-overlapping, case-sensitive occurrences and accepts optional start and end positions. Normalize with casefold() for case-insensitive counts, use startswith in a loop or a lookahead regex for overlapping matches, and split the text into words when you need whole-word counts. Remember that counting an empty string returns len(s) + 1.