I was working with the .count method for Strings. The count method finds the total number of occurrences of the argument inside the calling string.
But I don't understand how .count("")
works.
Can someone explain that?
I was working with the .count method for Strings. The count method finds the total number of occurrences of the argument inside the calling string.
But I don't understand how .count("")
works.
Can someone explain that?
There are three offsets – distances from the start of the string – where you can find an empty string: 0, 1, and 2.
| a | A |
^0 ^ ^
|1 |
|2
You can find an empty string at any offset for any string, of course, so s.count("")
is always len(s) + 1
.
The empty string occurs three times in "aA": once before the "a", once between the "a" and the "A" and once after the "A".