Python count() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

python
n = text.count("a")
CallWhat it does
"banana".count("a")Returns 3.
"aaa".count("aa")Returns 1 (non-overlapping).

Matches are case-sensitive and do not overlap.

Library declarations
python
str.count(sub, start=0, end=len(s)) → int

Parameters

ParameterDescription
subRequired. The substring to count.
startOptional. Index where the search starts. Default 0.
endOptional. Exclusive end index of the search range.

Return Value

Return ValueDescription
intThe number of non-overlapping matches.

Python Examples

Example 1 — Basic count()

Count how many times a word appears in a sentence.

python
text = "python is powerful, python is easy, python is fun"
word = "python"
total = text.count(word)

print(f"'{word}' appears {total} times.")

How It Works

  1. Set haystack and needle. Full sentence plus "python".
  2. Call count. Scans left to right for non-overlapping matches.
  3. Three hits. Each word match increments the total.
  4. Integer result. text itself never changes.

Example 2 — Using start and end

Limit the search to a slice—same rules as text[start:end].

python
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]))

How It Works

  1. Full scan. "ss" appears twice in "mississippi".
  2. Slice window. count("ss", 0, 5) only looks at text[0:5].
  3. Window content. That slice is "missi"—one "ss".
  4. 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.

python
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

How It Works

  1. First match. Indexes 0–1 form "aa".
  2. Skip past it. Search continues at index 2.
  3. No second match. Only one character left—overlap at index 1 is ignored.
  4. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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