Python | Count overlapping substring in a given string Last Updated : 30 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given a string and a sub-string, the task is to get the count of overlapping substring from the given string. Note that in Python, the count() function returns the number of substrings in a given string, but it does not give correct results when two occurrences of the substring overlap. Consider this example - Python3 string = "GeeksforGeeksforGeeksforGeeks" print(string.count("GeeksforGeeks")) Output: 2 The output we got here is 2, but the expected output is 3 since we also wanted to count the occurrence of overlapped sub-string. In order to solve this problem, we can use find() function in Python. It returns the start position of the first occurrence of substring in the given string, then we increment this position by 1 and continue the search from that position till the end of the string. Below is the implementation - Python3 def CountOccurrences(string, substring): # Initialize count and start to 0 count = 0 start = 0 # Search through the string till # we reach the end of it while start < len(string): # Check if a substring is present from # 'start' position till the end pos = string.find(substring, start) if pos != -1: # If a substring is present, move 'start' to # the next position from start of the substring start = pos + 1 # Increment the count count += 1 else: # If no further substring is present break # return the value of count return count # Driver Code string = "GeeksforGeeksforGeeksforGeeks" print(CountOccurrences(string, "GeeksforGeeks")) Output:3 Time complexity: O(n), where n is length of string.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Python | Count overlapping substring in a given string B BhavyadeepPurswani Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Find all combinations of overlapping substrings of a string Given a string, the task is to write a Python program to find all combinations of overlapping substrings of a string and store it in a list. The list of lists will be ordered and grouped by length of substrings. Input : test_str = 'Geeks4G' Output : [['', '', '', '', '', '', '', ''], ['G', 'e', 'e', 2 min read Python | Frequency of substring in given string Finding a substring in a string has been dealt with in many ways. But sometimes, we are just interested to know how many times a particular substring occurs in a string. Let's discuss certain ways in which this task is performed. Method #1: Using count() This is a quite straightforward method in whi 6 min read Count Occurance of Substring in a List of Strings - Python To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.Using sum() and Generator ExpressionThis method uses a generator 2 min read Python - Get all substrings of given string A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element 3 min read Python - Ways to Count Number of Substring in String Given a string s, determine the number of substrings that satisfy certain criteria. For example we are given a string s="hellohellohello" we need to count how many time the substring occur in the given string suppose we need to find the substring "hello" so that the count becomes 3. We can use metho 2 min read Python - Possible Substring count from String Given target string and argument substring, count how many substrings can be constructed using string characters, repetitions not allowed. Input : test_str = "geksefokesgergeeks", arg_str = "geeks" Output : 3 Explanation : "geeks" can be created 3 times using string characters. Input : test_str = "g 4 min read Python - All substrings Frequency in String Given a String, extract all unique substrings with their frequency. Input : test_str = "ababa" Output : {'a': 3, 'ab': 2, 'aba': 2, 'abab': 1, 'ababa': 1, 'b': 2, 'ba': 2, 'bab': 1, 'baba': 1} Explanation : All substrings with their frequency extracted. Input : test_str = "GFGF" Output : {'G': 2, 'G 5 min read Python | Get the starting index for all occurrences of given substring Given a string and a substring, the task is to find out the starting index for all the occurrences of a given substring in a string. Let's discuss a few methods to solve the given task. Method #1: Using Naive Method Python3 # Python3 code to demonstrate # to find all occurrences of substring in # a 3 min read Print Substrings that are Prefix of the Given String - Python The task of printing the substrings that are prefixes of a given string involves generating all the possible substrings that start from the first character of the string and end at any subsequent position.For example, if the given string is s = "hello", the prefixes of the string would include "h", 3 min read Python - All occurrences of substring in string A substring is a contiguous occurrence of characters within a string. Identifying all instances of a substring is important for verifying various tasks. In this article, we will check all occurrences of a substring in String.Using re.finditer()re.finditer() returns an iterator yielding match objects 3 min read Like