40

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?

Maroun
  • 94,125
  • 30
  • 188
  • 241
cjahangir
  • 1,773
  • 18
  • 27

3 Answers3

167

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

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 2
    Note that this answer refers to the deprecated `string.count()` function, not the equivalent [`str.count()` method](https://docs.python.org/2/library/stdtypes.html#str.count). – Matt Nordhoff Apr 08 '16 at 03:15
34

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

Community
  • 1
  • 1
mutilis
  • 563
  • 3
  • 18
  • 3
    Note that this answer refers to the deprecated `string.count()` function, not the equivalent [`str.count()` method](https://docs.python.org/2/library/stdtypes.html#str.count). – Matt Nordhoff Apr 08 '16 at 03:15
9

Just try to think it like:

In this word: "abacaba", how many non-overlapping "aba" words do you see? I see 2. And I also see a "c".

In this word: "www" how many non-overlapping "ww" words do you see? I see 1. And I also see a "w".

For a better explanation, think that you are deleting the instance when you see.

For "abacaba" you see "aba" and delete it. now there is "caba", you see "aba" again and delete it. now you get only "c". you see "aba" two times. It is same for the "www", you see "ww" once and delete it. now you see only "w". you have seen "ww" once only.

It makes sense.

pppery
  • 3,731
  • 22
  • 33
  • 46
cenk ebret
  • 687
  • 4
  • 15
  • 39
    intuition is a poor substitute for actually reading the docs – jk. Apr 07 '16 at 09:12
  • 4
    It's also inaccurate; "how many "ww" words do you see?" - I actually see 2, and 3 "w"s. You can restate it much better and more accurate. – Maroun Apr 07 '16 at 12:12
  • Ok, let me explain. think that you are deleting the instance when you see. For "abacaba" you see "aba" and delete it. now there is "caba", you see "aba" again and delete it. now you get only "c". you see "aba" two times. It is same for the "www", you see "ww" once and delete it. now you see only "w". you have seen "ww" once only. – cenk ebret Apr 07 '16 at 12:19
  • 3
    @cenkebret I advise you to edit your answer and clarify that. Your intuition to help is much appreciated, keep it going! – Maroun Apr 07 '16 at 12:28
  • thanks for your advice, i did it. – cenk ebret Apr 07 '16 at 12:30
  • I meant to say "intention to help", sorry. – Maroun Apr 07 '16 at 14:25
  • 1
    This explains a way of understanding what a non-overlapping occurrence is, but it doesn't say anything about how **count** is *supposed* to work. Is **count** supposed to count non-overlapping occurrences or overlapping occurrences? To answer that you *need* to consult some documentation. (Some experimentation might provide evidence for which it does, but even then you still can only know what a particular implementation is doing, but won't know whether it's performing correctly, or has a bug.) – Joshua Taylor Apr 07 '16 at 14:38