I am attempting to write a code snippet that requests from the user to enter a string s and then a substring ss. The program will then have to count the number of occurrences of ss in s. For example if the user enters s = ‘azcbobobegghakl’ and ss = ‘bob’, then the program should print: Number
of times bob occurs is: 2.
Here is my code so far :
def count(s,ss):
Occurrence = 0
if ss in s :
for ss in s :
Occurrence += 1
return Occurrence
#Main program :
s = str(input("Choose a string: "))
ss = str(input("Choose a substring:"))
print ("Number of times " + str(ss) + " occurs is : " + str(count(s,ss)) )
My desired output would be this:
Choose a string: hellohel
Choose a substring:hel
Number of times hel occurs is : 2
But instead I get this :
Choose a string: hellohel
Choose a substring:hel
Number of times hel occurs is : 8
So can someone please help me modify this code to deliver the desire output? Thanks in advance