[Python-ideas] string.replace should accept a list as a first argument

Sven R. Kunze srkunze at mail.de
Tue Oct 6 19:35:03 CEST 2015


On 06.10.2015 21:25, Emil Rosendahl Petersen wrote:
> I think string.replace should be changed accept a list as a first
> argument.

I think I can understand the sentiment here.

However, I would prefer a str.replace_all method that would accept it. I 
cannot think of any place, where I would want to use either a str or a 
list of str. So, this could hide bugs.


Speaking of "replace", sometimes, I would love to pass an "replace all 
of these with all of those" dict, which is then processed internally. I 
can remember two times where we needed to write some kind of for-loop; 
which actually might produce wrong results:

convert_dict = {
     '1': '2',
     '2': '3',
}
original = '12345'
for from_, to in convert_dict.items():
     original = original.replace(from_, to)

Real-world examples don't really have this issue so we accepted this 
kind of workaround for smaller scripts. But I would prefer a stdlib 
solution for this.


Best,
Sven


More information about the Python-ideas mailing list