Python count() Method

Definition
In Python, count() returns how many non-overlapping times a substring appears. If none are found, it returns 0.
sub is required. Optional start and end limit the search to a slice, like text[start:end].
Syntax
Call count with the substring. Start with this line.
| Call | What it does |
|---|
"banana".count("a") | Returns 3. |
"aaa".count("aa") | Returns 1 (non-overlapping). |
Matches are case-sensitive and do not overlap.
Library declarations
str.count(sub, start=0, end=len(s)) → int
Parameters
| Parameter | Description |
|---|
| sub | Required. The substring to count. |
| start | Optional. Index where the search starts. Default 0. |
| end | Optional. Exclusive end index of the search range. |
Return Value
| Return Value | Description |
|---|
| int | The number of non-overlapping matches. |
Python Examples
Example 1 — Basic count()
Count how many times a word appears in a sentence.
text = "python is powerful, python is easy, python is fun"
word = "python"
total = text.count(word)
print(f"'{word}' appears {total} times.")
'python' appears 3 times.
How It Works
- Set haystack and needle. Full sentence plus
"python". - Call count. Scans left to right for non-overlapping matches.
- Three hits. Each word match increments the total.
- Integer result.
text itself never changes.
Example 2 — Using start and end
Limit the search to a slice—same rules as text[start:end].
text = "mississippi"
full = text.count("ss")
partial = text.count("ss", 0, 5)
print("Full string:", full)
print("Index 0–5: ", partial)
print("Slice 0–5: ", repr(text[0:5]))
Full string: 2
Index 0–5: 1
Slice 0–5: 'missi'
How It Works
- Full scan.
"ss" appears twice in "mississippi". - Slice window.
count("ss", 0, 5) only looks at text[0:5]. - Window content. That slice is
"missi"—one "ss". - end is exclusive. Index 5 is not included, just like normal slicing.
Example 3 — Non-Overlapping Matches
Why "aaa".count("aa") is 1, not 2.
text = "aaa"
sub = "aa"
print("Text: ", repr(text))
print("Sub: ", repr(sub))
print("Count:", text.count(sub))
# case tip:
print("Case:", "Hi Hi".count("hi")) # 0 — case-sensitive
Text: 'aaa'
Sub: 'aa'
Count: 1
Case: 0
How It Works
- First match. Indexes
0–1 form "aa". - Skip past it. Search continues at index
2. - No second match. Only one character left—overlap at index 1 is ignored.
- Case tip.
"Hi" ≠ "hi"; fold with casefold() if you need insensitive counts.
Frequently Asked Questions
It returns the number of non-overlapping occurrences of a substring.
Yes. Uppercase and lowercase must match.
No. "aaa".count("aa") returns 1.
They limit the search to a slice from start up to—but not including—end.
count() returns a frequency. find() returns the first index, or -1.
Next: encode()
Turn text into bytes with a chosen encoding (UTF-8 and more).
encode() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful