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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original string is : GeeksforGeeks is for Geeks
The original substring : Geeks
The frequency of substring in string is 3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.