replace from end of string?

Brian van den Broek bvande at po-box.mcgill.ca
Wed Mar 16 15:40:53 EST 2005


Hi all,

in writing my response to Mark Leeds' recent question about replacing 
text at the end of a string, I was moved to wonder if it wouldn't it 
be handy if the replace method of string instances did one of the 
following:

took a negative integer for count and interpreted that as replace the 
absolute value of count many instance, but starting from the end of 
the string, or,

had a default parameter from_end=False, such that if it were True, 
replacements happened count many times, starting from the end?


I get that it isn't too much of a hassle to write a function to do 
this (heck, *I* did it, so it cannot be too rough ;-). But, it seems a 
common enough task to be a useful addition nevertheless. (And the 
speed of the C implementation wouldn't hurt, either.)

Is there already a pre-rolled way to do this that I've overlooked? Or 
some strong reason there oughtn't be?

Thanks, and best to all,

Brian vdB




More information about the Python-list mailing list