[New-bugs-announce] [issue28029] Replace and empty strings

Stéphane Henriot report at bugs.python.org
Thu Sep 8 16:30:41 EDT 2016


New submission from Stéphane Henriot:

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.

----------
messages: 275140
nosy: Stéphane Henriot
priority: normal
severity: normal
status: open
title: Replace and empty strings
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28029>
_______________________________________


More information about the New-bugs-announce mailing list