Author Stéphane Henriot
Recipients Stéphane Henriot
Date 2016-09-08.20:30:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1473366641.62.0.989746465638.issue28029@psf.upfronthosting.co.za>
In-reply-to
Content
A few days ago, the following behavior surprised me.

>>> "".replace("", "prefix", 1)
''
>>> "".replace("", "prefix")
'prefix'

It seems to me this edge case isn't correctly documented/implemented. I tested with python 2.7, 3.4 and 3.5, I guess it applies for other versions as well.

Here are a few elements, for reference.

« string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced. »

>>> "" in ""
True
>>> "".count("")
1
>>> "".find("")
0

https://hg.python.org/cpython/file/2.7/Objects/stringobject.c#l2750
https://hg.python.org/cpython/file/default/Objects/unicodeobject.c#l10479

Thanks,

Stéphane.
History
Date User Action Args
2016-09-08 20:30:41Stéphane Henriotsetrecipients: + Stéphane Henriot
2016-09-08 20:30:41Stéphane Henriotsetmessageid: <1473366641.62.0.989746465638.issue28029@psf.upfronthosting.co.za>
2016-09-08 20:30:41Stéphane Henriotlinkissue28029 messages
2016-09-08 20:30:41Stéphane Henriotcreate