2

When I tried this code on a python command line:

>>> a = "hello world"
>>> print (a.count(""))

I have as a result:

12

Someone know what is the meaning of this number?

Can I assume that it is the same as using the "len()" built-in function by subtracting a unit?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Diroallu
  • 824
  • 1
  • 11
  • 15

2 Answers2

1

The answer lies in the below code

"Hello World".split("")
# returns ['H', 'e' , 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

This is how it splits the string So, look it at this way "" is the space or no space between each character in the string (including spaces). So start counting from before H and count on each side of char and space and you will understand why it was 12.

Bharat23
  • 527
  • 3
  • 15
1

Take a look at this helpful answer on the difference between len(a) and a.count("") too.

Why are str.count('') and len(str) giving different output?

Homayoun
  • 11
  • 3