1

I was going through something when i came across this...

a = 'hello'

print(a.count('l'))

print(a.count(''))

Output is :

2

6

Can anyone explain why the second output is 6 ?

1 Answers1

0

count here returns the number of occurences of your substring, here a '' string.

There are indeed six '' in hello, between each char:

print(''.count(''))  # >> 1

# You could symbolize it like that:
print('a'.count(''))  # >> 2

# is equivalent to:
print('|a|'.count('|'))  # >> 2
olinox14
  • 6,177
  • 2
  • 22
  • 39