liberal liberal - 9 months ago 35
Python Question

Why are string.count('') and len(string) giving different output in python 3.5?

Look at following code and Please explain why count() and len() functions are giving two different outputs.




Answer Source

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('')

That's because empty strings are considered to exist between all the characters of a string; so 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.