Python - Ways to Count Number of Substring in String Last Updated : 18 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 methods like count(). Using str.count()count() method returns the number of non-overlapping occurrences of a substring in the string. Python # Define the input string and the substring to count s = "hellohellohello" sub = "hello" # Use the count() method to find the number of occurrences of the substring c = s.count(sub) print(c) Output3 Explanation:count() method counts the number of non-overlapping occurrences of the substring "hello" within the string s.In this case, it returns 3 because the substring "hello" appears three times in "hellohellohello".Using a For Loop with String SlicingA for loop can iterate through the string, using slicing to check for the occurrence of a substring. For example, using s[i:i+len(substring)] inside the loop allows for comparing each slice of the string to the target substring Python s1 = "hellohellohello" sub = "hello" # Initialize count to zero cnt = 0 # Iterate through the string to check for the substring for i in range(len(s) - len(sub) + 1): if s[i:i + len(sub)] == sub: # If substring matches, increment count cnt += 1 print(cnt) Output3 Explanation:for loop iterates through the string s, using slicing (s[i:i+len(substring)]) to extract substrings of the same length as substring and compare them.If the sliced substring matches the target substring, the count is incremented, resulting in a count of 3 for the occurrences of "hello" in "hellohellohello".Using List ComprehensionGenerate a list of indices where the substring starts and count its length. Python # Input string and substring to count s = "hello world, hello universe" sub = "hello" # Count occurrences of the substring using a generator expression c = sum(1 for i in range(len(s) - len(sub) + 1) if s[i:i + len(sub)] == sub) print(c) Output2 Explanation:Generator expression iterates through all possible starting indices in s and counts matches of sub with slices of s.Range ensures slicing stays within bounds to compare each substring correctly. Comment More infoAdvertise with us Next Article Python - Ways to Count Number of Substring in String garg_ak0109 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python | Ways to find nth occurrence of substring in a string Given a string and a substring, write a Python program to find the nth occurrence of the string. Let's discuss a few methods to solve the given task. Get Nth occurrence of a substring in a String using regex Here, we find the index of the 'ab' character in the 4th position using the regex re.findit 4 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 - 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 | Count overlapping substring in a given string 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 thi 2 min read Python program to count the number of spaces in string In Python, there are various ways to Count the number of spaces in a String.Using count() Methodcount() method in Python is used to return the number of occurrences of a specified element in a list or stringPythons = "Count the spaces in this string." # Count spaces using the count() method space_co 3 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 Count the number of characters in a String - Python The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is 2 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 Frequency of Numbers in String - Python We are given a string and we have to determine how many numeric characters (digits) are present in the given string. For example: "Hello123World456" has 6 numeric characters (1, 2, 3, 4, 5, 6).Using re.findall() re.findall() function from the re module is a powerful tool that can be used to match sp 3 min read Like