Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "www".count("ww") return 1 and not 2? [duplicate]

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?

like image 459
cjahangir Avatar asked Apr 07 '16 06:04

cjahangir


2 Answers

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".

like image 139
Maroun Avatar answered Oct 22 '22 12:10

Maroun


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

like image 34
mutilis Avatar answered Oct 22 '22 10:10

mutilis