Python String count() Method
The Python string count() method returns how many times a substring appears in a string.
This reference page explains:
- what
count()does - its syntax
- what it returns
- how
startandendwork - common beginner mistakes
Quick example
text = "banana"
print(text.count("a"))
print(text.count("na"))
Output:
3
2
Use string.count(value) to count how many times a substring appears in a string.
What the count() method does
count() tells you how many times some text appears inside a string.
Key points:
- It returns how many times a substring appears in a string
- It works with single characters and longer strings
- It returns an integer
- It does not change the original string
For example, if you want to know how many "a" characters are in "banana", count() is a simple choice.
Basic syntax
string.count(value, start, end)
Parameters
value: the text to search forstart: optional; where counting beginsend: optional; where counting stops
start and end work like slice positions:
startis includedendis not included
Return value
count() returns a whole number.
Examples:
- If the substring is found 3 times, it returns
3 - If the substring is not found, it returns
0
It does not return True or False.
If you only want to check whether text exists, using the in operator is often a better fit.
Simple example
Here is a short example that counts one character:
text = "apple"
result = text.count("p")
print(result)
Output:
2
This means "p" appears 2 times in "apple".
Counting a word or substring
count() can search for more than one character. It can count full words or repeated patterns.
text = "cat dog cat bird cat"
print(text.count("cat"))
Output:
3
It matches exact text only. That means spacing, punctuation, and letter case matter.
Example:
text = "one One one"
print(text.count("one"))
Output:
2
The uppercase "One" is not counted because count() is case-sensitive.
Using start and end
You can limit the search to only part of the string.
text = "banana"
print(text.count("a", 2))
print(text.count("a", 2, 5))
Output:
2
1
How this works
In "banana":
- index
2starts at the first"n" text.count("a", 2)counts from index2to the endtext.count("a", 2, 5)counts from index2up to, but not including, index5
This is similar to slicing:
text = "banana"
print(text[2:5])
Output:
nan
Only that section is searched.
Case sensitivity
count() is case-sensitive.
That means:
"a"and"A"are different"hello"and"Hello"are different
Example:
text = "Apple apple APPLE"
print(text.count("apple"))
Output:
1
If you want case-insensitive counting, convert the string first with lower():
text = "Apple apple APPLE"
print(text.lower().count("apple"))
Output:
3
Common mistakes
Here are some common beginner mistakes when using count().
Expecting count() to ignore case automatically
This will not count uppercase and lowercase as the same:
text = "Aardvark"
print(text.count("a"))
Output:
2
The uppercase "A" at the start is not counted.
If needed, convert first:
text = "Aardvark"
print(text.lower().count("a"))
Forgetting quotes around the substring
This causes an error if the variable does not exist:
text = "banana"
print(text.count(a))
Python tries to find a variable named a.
Correct version:
text = "banana"
print(text.count("a"))
Confusing count() with find() or index()
Use count() when you need the number of matches.
Use find() when you need the position of the first match.
Use index() when you also need the position, but want an error if the text is not found.
Assuming it counts overlapping matches
count() counts non-overlapping matches only.
Example:
text = "aaaa"
print(text.count("aa"))
Output:
2
It does not return 3.
Why not?
Because Python counts these matches:
"aa"at positions0-1"aa"at positions2-3
It does not count overlapping matches like positions 1-2.
When to use count()
Use count() when you need the number of matches in a string.
Choose the right tool for the job:
- Use
count()when you need how many times something appears - Use
find()when you need where it appears - Use the
inoperator when you only need to know if it exists - Use
replace()when you want to change matching text
Debugging tips
If count() is not giving the result you expect, check these things:
print(text)
print(type(text))
print(text.count("a"))
print(text.lower().count("a"))
print(text.count("na"))
These checks can help you find problems such as:
- the string using different uppercase and lowercase letters
textnot being a string- counting the wrong substring
- expecting overlapping matches
Common causes of confusion include:
- trying to count text with different letter case
- passing a variable name that was not defined
- expecting overlapping matches to be counted
- using
count()whenfind()orinwould be a better fit
FAQ
Does string count() change the original string?
No. It only returns a number. The original string stays the same.
Is count() case-sensitive?
Yes. Uppercase and lowercase letters are counted separately.
What does count() return if nothing is found?
It returns 0.
Can count() count words instead of letters?
Yes. It can count any substring, including full words.
Does count() count overlapping matches?
No. It counts non-overlapping matches only.