Python - Ways to Count Number of Substring in String Last Updated : 12 Jul, 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 G garg_ak0109 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 7 min read Python Variables 6 min read Python Operators 6 min read Python Keywords 2 min read Python Data Types 9 min read Conditional Statements in Python 6 min read Loops in Python - For, While and Nested Loops 9 min read Python Functions 9 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like