str.count()

str.count(sub[, start[, end]])
Returns: int · Updated March 13, 2026 · String Methods
strings counting methods

The count() method searches for a substring and returns how many times it appears. It only counts non-overlapping occurrences, meaning each match starts after the previous one ends.

Syntax

str.count(sub[, start[, end]])

Parameters

ParameterTypeDefaultDescription
substrThe substring to search for
startintNoneIndex to start searching from (defaults to 0)
endintNoneIndex to stop searching at (defaults to len(string))

Examples

Basic usage

text = "hello hello hello"
print(text.count("hello"))
# 3

print(text.count("world"))
# 0

Using start and end

text = "abababab"
print(text.count("ab"))
# 4

# Only count from index 4 onwards
print(text.count("ab", 4))
# 2

Case sensitivity

text = "Hello hello HELLO"
print(text.count("Hello"))
# 1

print(text.count("hello"))
# 1

print(text.lower().count("hello"))
# 3

Common Patterns

Counting characters

text = "the quick brown fox jumps over the lazy dog"
vowels = sum(1 for c in text if c in "aeiou")
print(vowels)
# 11

Counting words in a sentence

sentence = "the cat sat on the mat"
word = "the"
count = sentence.split().count(word)
print(count)
# 2

Checking if a substring appears

text = "import this"
if text.count("import") > 0:
    print("Found 'import'")
# Found 'import'

See Also