Advertisement
Programmin-in-Python

Recursive function to count the occurences of a substring in a string

Oct 23rd, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.21 KB | None | 0 0
  1. def count_occurences(s, sub):
  2.     if len(s) == 0:
  3.         return 0
  4.     else:
  5.         ind = s.find(sub)
  6.  
  7.         if ind>=0:
  8.             return 1+count_occurences(s[ind+1:], sub)
  9.         else:
  10.             return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement