I know that str.count(sub) return the number of occurrences of substring sub.
But I faced a strange problem that I cannot understand.
My code is as below:
str = 'helloworld'
print(str.count(''))
>>>11
The output is puzzling. Why does it return 11?
If '' means any letters, why not return the length of it (10)?
I haven't found any answers about this.
Can anybody tell me about the implementation?
''doesn't mean any letters, it means an empty string. There are 11 empty strings in'helloworld', one at the start, nine between each pair of letters and one at the end. If you want the length, uselen('helloworld'). - jonrsharpe"foo"as being equal to"" + "f" + "" + "o" + "" + "o" + "", which is in some sense the minimal number of empty strings to be found contained in the string"foo". - chepner