Python | Frequency of substring in given string
Finding a substring in a string has been dealt 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 which this task is performed. It simply counts the occurrence of substrings in a string that we pass as an argument.
# Python3 code to demonstrate working of # Frequency of substring in string # Using count() # initializing string test_str = "GeeksforGeeks is for Geeks" # initializing substring test_sub = "Geeks" # printing original string print("The original string is : " + test_str) # printing substring print("The original substring : " + test_sub) # using count() # Frequency of substring in string res = test_str.count(test_sub) # printing result print("The frequency of substring in string is " + str(res)) |
The original string is : GeeksforGeeks is for Geeks The original substring : Geeks The frequency of substring in string is 3
Method #2 : Using len() + split()
The combination of above functions can be used to perform this task. This is performed in 2 steps, in 1st step, we split the string to list by the substring and then count the elements, which is 1 more than the required value.
# Python3 code to demonstrate working of # Frequency of substring in string # Using split() + len() # initializing string test_str = "GeeksforGeeks is for Geeks" # initializing substring test_sub = "Geeks" # printing original string print("The original string is : " + test_str) # printing substring print("The original substring : " + test_sub) # using split() + len() # Frequency of substring in string res = len(test_str.split(test_sub))-1 # printing result print("The frequency of substring in string is " + str(res)) |
The original string is : GeeksforGeeks is for Geeks The original substring : Geeks The frequency of substring in string is 3
Recommended Posts:
- Python | Frequency of each character in String
- Find frequency of each word in a string in Python
- Python | Construct string from character frequency tuple
- Python | Count all prefixes in given string with greatest frequency
- Python | Get the string after occurrence of given substring
- Python | All occurrences of substring in string
- Python | Remove the given substring from end of string
- Python | Check if a Substring is Present in a Given String
- Python | Count overlapping substring in a given string
- Python | Check if substring present in string
- Python | Get the substring from given string using list slicing
- Python | Ways to count number of substring in string
- Python | Ways to find nth occurrence of substring in a string
- Python | List frequency of elements
- Python | Extract least frequency element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


