0

The count function of strings return the number of non-overlapping occurrences of substring. However, when I try to count empty string in non-empty or empty string, it does not give 0, but len(str) + 1.

>>> 'aaa'.count('') # it should have been 0
>>> 4
>>> ''.count('') # it should have been 0
>>> 1

What is the logic behind this?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

1 Answers1

1

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.

original answer