Pay attention to the following interactive session:
In [1]: s = 'emptiness'
In [2]: s.replace('', '*')
Out[2]: '*e*m*p*t*i*n*e*s*s*'
In [3]: s.count('')
Out[3]: 10
I discovered this today and it is a bit confusing and surprising to me.
I like to learn things like Python, but it looks like this might lead to some pretty confusing errors. For example, if an empty string was passed as a variable and just turned out to be an empty string, you might get some unexpected consequences. The behavior also seems a little inconsistent because, based on the interactive session above, I would have thought that the following would lead to a list of all the characters in the string (similar to JavaScript behavior). Instead, you get an error message:
In [4]: s.split('')
ValueError Traceback (most recent call last)
<ipython-input-4-c31bd2432bc1> in <module>()
ValueError: empty separator
In addition, this leads to some seemingly contradictory behavior with str.endswith()and str.startswith():
In [5]: s.endswith('')
Out[5]: True
In [6]: s.endswith('s')
Out[6]: True
In [7]: s.startswith('')
Out[7]: True
In [8]: s.startswith('e')
Out[8]: True
, .
: ? , str ? - - / , .