[Python-ideas] Revisiting str.rreplace()

Eric V. Smith eric at trueblade.com
Thu Jul 19 11:17:13 EDT 2018


On 7/19/2018 10:01 AM, Calvin Spealman wrote:
> As an alternative suggestion: What if the count parameter to 
> str.replace() counted from the right with negative values? That would be 
> consistent with other things like indexing and slicing.

We couldn't make this change because negative values already have a 
meaning: it's interpreted as a missing parameter:

 >>> 'abab'.replace('a', 'z')
'zbzb'
 >>> 'abab'.replace('a', 'z', 0)
'abab'
 >>> 'abab'.replace('a', 'z', 1)
'zbab'
 >>> 'abab'.replace('a', 'z', -1)
'zbzb'
 >>> 'abab'.replace('a', 'z', -2)
'zbzb'
 >>> 'abab'.replace('a', 'z', -100)
'zbzb'

I think .rreplace() is the better design.

Eric


More information about the Python-ideas mailing list