Reputation: 21
I'm trying to write a program which counts how many times a substring appears within a string.
word = "wejmfoiwstreetstreetskkjoih"
streets = "streets"
count = 0
if streets in word:
count += 1
print(count)
as you can see "streets" appears twice but the last s of streets is also the beginning of streets. I can't think of a way to loop this.
Thanks!
Upvotes: 2
Views: 187
Reputation: 34312
A generic (though not as elegant) way would be a loop like this:
def count_substrings(stack, needle):
idx = 0
count = 0
while True:
idx = stack.find(needle, idx) + 1 # next time look after this idx
if idx <= 0:
break
count += 1
return count
My measurement shows that it's ~8.5 times faster than the solution with startswith
for every substring.
Upvotes: 0
Reputation: 43364
Can be done using a regex
>>> import re
>>> text = 'streetstreets'
>>> len(re.findall('(?=streets)', text))
2
From the docs:
(?=...)
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
Upvotes: 4
Reputation: 44464
Quick and dirty:
>>> word = "wejmfoiwstreetstreetskkjoih"
>>> streets = "streets"
>>> sum(word[start:].startswith(streets) for start in range(len(word)))
2
Upvotes: 2