My question is that why python giving the ans is 7 without having the string in the count method
txt = "banana"
x = txt.count("")
print(x)
My question is that why python giving the ans is 7 without having the string in the count method
txt = "banana"
x = txt.count("")
print(x)
It can be fix by passing ' '
instead of ''
:
>>> 'banana'.count()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: count() takes at least 1 argument (0 given)
>>> 'banana'.count('')
7
>>> len('banana' ) + len('!')
7
>>> 'banana'.count( ' ')
0
>>> 'banana'.swapcase().count('B')
1
Q: Why it's printing the 7?
A: The len of an input string ("bnanana" == 6) + one extra character (1) --> total: 7