Use Python’s str.count() method to find how many times a substring occurs in a string. The method counts non-overlapping matches and can optionally limit the search to a specified range of character positions.
Count Substring Occurrences in a Python String
Call the count() method on the main string and pass the substring to search for. The method returns an integer containing the number of non-overlapping occurrences.
string.count(substring)
For example, 'one two one'.count('one') returns 2.
Python str.count() Syntax with Start and End Positions
The complete syntax of the method is:
string.count(substring, start, end)
substring: The text to count. This argument is required.start: The optional index at which the search begins. The character at this index is included.end: The optional index at which the search stops. The character at this index is excluded.
The start and end arguments follow the same indexing rules as Python string slicing.
Return Value of str.count()
The count() method returns an integer. It returns 0 when the specified substring is not found. It does not modify the original string.
Python Examples for Counting a Substring
1. Count a Substring Without Overlapping Matches
In the following program, the substring Hello appears two times in the main string.
Python Program
#the string
str = 'Hello World. Hello TutorialKart.'
#substring
substr = 'Hello'
#finding number of occurrences of substring in given string
count = str.count(substr)
print("Number of occurrences of substring :", count)
Output
Number of occurrences of substring : 2
The method finds two non-overlapping matches and therefore returns 2.
2. Understand How str.count() Handles Overlapping Substrings
Python’s count() method does not include overlapping matches. For example, the substring aa can begin at adjacent positions in a sequence of repeated a characters, but count() advances past each complete match before looking for the next one.
Python Program
#the string
str = 'aaaaaaaa'
#substring
substr = 'aa'
#finding number of occurrences of substring in given string
count = str.count(substr)
print("Number of occurrences of substring :", count)
Output
Number of occurrences of substring : 4
The eight-character string is divided into four non-overlapping occurrences of aa.
3. Count a Substring Within a Specific Index Range
Pass start and end indexes when only part of the string should be searched. In this example, the first occurrence is outside the selected range.
text = 'red blue red green red'
count = text.count('red', 4, 22)
print(count)
Output
2
The search begins at index 4 and stops before index 22, so only the second and third occurrences are counted.
4. Count a Substring Without Case Sensitivity
String matching is case-sensitive by default. Convert both strings to the same case when uppercase and lowercase forms should be treated as equal.
text = 'Python python PYTHON'
substring = 'python'
count = text.lower().count(substring.lower())
print(count)
Output
3
Calling lower() creates lowercase versions for comparison. The original values remain unchanged.
Count Overlapping Substring Occurrences in Python
Use a loop with str.find() when overlapping occurrences must be included. After finding a match, move the search position forward by one character instead of moving past the complete substring.
text = 'aaaa'
substring = 'aa'
count = 0
position = 0
while True:
position = text.find(substring, position)
if position == -1:
break
count += 1
position += 1
print(count)
Output
3
The substring aa starts at indexes 0, 1, and 2, producing three overlapping matches.
Important str.count() Behaviors
- Matching is case-sensitive:
'Python'.count('python')returns0. - Matches do not overlap:
'aaaa'.count('aa')returns2, not3. - An absent substring returns zero: No exception is raised when a match is not found.
- An empty substring has special behavior:
'abc'.count('')returns4because Python counts the positions before, between, and after the characters. - The original string is unchanged: Python strings are immutable, and
count()only returns a result.
Frequently Asked Questions About Python Substring Counting
Does Python str.count() include overlapping occurrences?
No. The method counts only non-overlapping occurrences. Use a loop with find() or another matching technique when overlapping occurrences are required.
Is substring counting case-sensitive in Python?
Yes. Uppercase and lowercase characters are treated as different values. Convert the main string and substring with lower() or casefold() for a case-insensitive comparison.
How do I count a substring only within part of a string?
Provide optional start and end indexes, as in text.count(substring, start, end). The start position is included, while the end position is excluded.
What does str.count() return when the substring is missing?
It returns the integer 0. It does not raise an exception merely because the substring is absent.
Summary of Counting Substrings in Python
Use str.count() for a direct count of non-overlapping substring occurrences. Add start and end indexes to restrict the search, normalize case before counting for case-insensitive matching, and use a custom find() loop when overlapping matches must be included. Continue with the main Python Tutorial for more string operations and examples.
TutorialKart.com