On Tue, Jul 2, 2019 at 4:24 PM email@example.com wrote:
It seems to me that the desired behavior here is closest to 'str.replace()' out of all the options discussed, just with the constraint of being limited to either the start or the end of the string. (Thus the .lreplace() and .rreplace() option suggested by Glenn.)
The minimal change (which actually also is pretty general?) I think would be to add 'only_start' and 'only_end' keyword arguments to str.replace(), both defaulting to False. If, e.g., 'only_start' is passed True, each repetition of 'old' at the start of 's' is replaced by 'new', with the number of replacements limited by the existing optional 'count'. Similar behavior for 'only_end'=True. Probably best to raise a ValueError(?) if both 'only_start'=True and 'only_end'=True?
Taking swapping a file extension as an example of a particular transformation of interest, it would be achieved with something like s.replace(".htm", ".html", only_end=True).
Just to add my own touch of colour to the bike shed, I'd suggest that more naturally-usable names for those arguments, should they be added, would be at_start and at_end.