For this code,
f='ana'
g='banana'
print(g.count(f))
Why it gives 1 as output and not 2.Is their any mistake in my code. Also could anyone suggest any alternate method.
For this code,
f='ana'
g='banana'
print(g.count(f))
Why it gives 1 as output and not 2.Is their any mistake in my code. Also could anyone suggest any alternate method.
Per the answers to this question which is a duplicate of your question, a potential approach would be:
def str_count(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start > 0:
count+=1
else:
return count