[issue28029] Replace and empty strings

Serhiy Storchaka report at bugs.python.org
Tue Oct 29 03:56:20 EDT 2019


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

There are expected some relations between str methods. For example,

s.replace(a, b, n) == s.replace(a, b)   if n >= s.count(a)
len(s.replace(a, b, n)) == len(s) + (len(b)-len(a)) * n   if 0 <= n <= s.count(a)
len(s.replace(a, b, n)) == len(s) + (len(b)-len(a)) * s.count(a)   if n >= s.count(a)

Inconsistency between "".replace("", s, n) and "".replace("", s) is just a bug, and it should be fixed in the most consistent way. There are precedences, the behavior of replace() already was changed 3 times in the past. I think that chances to break some code are tiny, we just fix inconsistency why can puzzle users.

----------

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


More information about the Python-bugs-list mailing list