Just started learning Python. Had to create a function to count a sub-string within a string. Decided to use the count() function of the string module but it doesn't do what I was hoping for.
It seems the count() function will iterate through string and if it does find the sub-string, it will move add to the count but will continue it's iteration at the end of the sub-string.
Below is the code and the test I was running:
def count(substr,theStr):
counter = 0
counter = theStr.count(substr, 0, len(theStr))
return counter
print(count('is', 'Mississippi'))
# Expected count: 2 pass
print(count('an', 'banana'))
# Expected count: 2 pass
print(count('ana', 'banana'))
# Expected count: 2 test failed: count: 1
print(count('nana', 'banana'))
# Expected count: 1 pass
print(count('nanan', 'banana'))
# Expected count: 0 pass
print(count('aaa', 'aaaaaa'))
# Expected count: 5 test failed: count: 2