s = 'your'
print(s.count(''))
I expected the output to be 4, but it gave me an output 5. What's the logic behind it?
s = 'your'
print(s.count(''))
I expected the output to be 4, but it gave me an output 5. What's the logic behind it?
I'm not sure why you expect 4. There are 5 empty strings "in" s
, with (conventionally) one in each of the following positions:
y
y
and o
o
and u
u
and r
r
If you were only expecting empty strings in the interior, you should have expected 3, ignoring both the first and last occurrences.
(I say "conventionally", because really, you could claim there are infinite empty strings in any of the 5 positions, since x + "" == "" + x == x
. But since assuming one empty string per slot is sufficient to let all our nice laws about string concatenation hold, we make the simplest assumption.)