Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to count all occurrences of a substring with/without overlapping matches

In Python, you can count the occurrences of a substring in a string with and without overlapping matches using different approaches. Here, we'll demonstrate two methods for each case.

  • Count occurrences without overlapping matches using the count() method:

The count() method of a string counts the non-overlapping occurrences of a substring in the string.

text = "ABCABCABC"
substring = "ABC"

count = text.count(substring)
print(f"Count without overlapping: {count}")

Output:

Count without overlapping: 3
  • Count occurrences with overlapping matches using a loop:

To count occurrences with overlapping matches, you can use a loop to iterate through the string and check for the substring at each position.

text = "ABCABCABC"
substring = "ABC"

count = sum(1 for i in range(len(text) - len(substring) + 1) if text[i:i + len(substring)] == substring)
print(f"Count with overlapping: {count}")

Output:

Count with overlapping: 3

In this example, we use a generator expression with the sum() function to count the overlapping occurrences of the substring. The generator iterates through the string and checks if the slice text[i:i + len(substring)] is equal to the substring. If it is, it contributes 1 to the sum.

  • Count occurrences without overlapping matches using a regular expression:

You can also use the re module to count non-overlapping occurrences of a substring using regular expressions.

import re

text = "ABCABCABC"
substring = "ABC"

count = len(re.findall(substring, text))
print(f"Count without overlapping: {count}")

Output:

Count without overlapping: 3
  • Count occurrences with overlapping matches using a regular expression:

To count overlapping occurrences with regular expressions, you can use a lookahead assertion in the pattern.

import re

text = "ABCABCABC"
substring = "ABC"

pattern = f"(?={substring})"
count = len(re.findall(pattern, text))
print(f"Count with overlapping: {count}")

Output:

Count with overlapping: 3

In this example, we use the (?=...) syntax for the lookahead assertion, which checks if the substring follows the current position without actually consuming it. This allows the regular expression to find overlapping matches.

  1. Python count substring occurrences in a string:

    main_string = "python is powerful. python is easy."
    substring = "python"
    count = main_string.count(substring)
    print(count)
    
  2. Find all occurrences of a substring and count in Python:

    main_string = "python is powerful. python is easy."
    substring = "python"
    occurrences = [i for i in range(len(main_string)) if main_string.startswith(substring, i)]
    count = len(occurrences)
    print(count)
    
  3. Count substring occurrences using regular expressions in Python:

    import re
    
    main_string = "python is powerful. python is easy."
    substring = "python"
    count = len(re.findall(substring, main_string))
    print(count)
    
  4. Python loop for counting non-overlapping substring matches:

    main_string = "python is powerful. python is easy."
    substring = "python"
    count = 0
    index = 0
    while index != -1:
        index = main_string.find(substring, index)
        if index != -1:
            count += 1
            index += len(substring)
    print(count)
    
  5. Python count overlapping substring occurrences:

    main_string = "python is powerful. python is easy."
    substring = "python"
    count = sum(1 for i in range(len(main_string) - len(substring) + 1) if main_string[i:i+len(substring)] == substring)
    print(count)
    
  6. Using itertools for counting overlapping substring occurrences in Python:

    from itertools import count, islice
    
    main_string = "python is powerful. python is easy."
    substring = "python"
    count = sum(1 for _ in islice(count(), main_string.find(substring), None, len(substring)) if main_string[_:_+len(substring)] == substring)
    print(count)
    
  7. Count occurrences of a substring with sliding window in Python:

    main_string = "python is powerful. python is easy."
    substring = "python"
    count = sum(main_string[i:i+len(substring)] == substring for i in range(len(main_string) - len(substring) + 1))
    print(count)
    
  8. Overlapping substring counting with re.finditer() in Python:

    import re
    
    main_string = "python is powerful. python is easy."
    substring = "python"
    count = sum(1 for _ in re.finditer(f'(?={re.escape(substring)})', main_string))
    print(count)