Why are str.count('') and len(str) giving different output?

Solution 1:

str.count() counts non-overlapping occurrences of the substring:

Return the number of non-overlapping occurrences of substring sub.

There is exactly one such place where the substring '' occurs in the string '': right at the start. So the count should return 1.

Generally speaking, the empty string will match at all positions in a given string, including right at the start and end, so the count should always be the length plus 1:

>>> (' ' * 100).count('')
101

That's because empty strings are considered to exist between all the characters of a string; for a string length 2, there are 3 empty strings; one at the start, one between the two characters, and one at the end.

So yes, the results are different and they are entirely correct.

Solution 2:

.count('') counts the number of locations of zero-length strings. You could also think of this as the number of possible cursor positions.

"test".count('')

 t e s t
^ ^ ^ ^ ^

Instead of counting the number of characters (like len(str)), you're counting the number of anti-characters.