In my code:
>> s = 'abacaba'
>> s.count('aba')
>> 2
For the above code I am getting the correct answer as 'aba' occurs 2 times in the string s
.
But for the following case:
>> s = 'www'
>> s.count('ww')
>> 1
In this case I am expecting that s.count('ww')
will return 2
. But it returns 1
.
Why?
Read the docs:
Return the number of (non-overlapping) occurrences of substring sub in string
s[start:end]
. Defaults for start and end and interpretation of negative values are the same as for slices.
Since "ww" is first matched, it proceeds from the third "w" and fails to match "ww".
string.count(s, sub[, start[, end]])
:Return the number of (non-overlapping) occurrences of substring sub in string s[start:end]. Defaults for start and end and interpretation of negative values are the same as for slices.
source: https://docs.python.org/2/library/string.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With