Python string replace the values

Steve D'Aprano steve+python at pearwood.info
Fri Sep 1 20:15:42 EDT 2017


On Sat, 2 Sep 2017 03:13 am, Ganesh Pal wrote:

> Example :
> 
>   "a0000" and "1" => a0001
> 
>   "a0000" and "aa" => c00aa

Why does the leading 'a' change to a 'c'? Is that a mistake? I'll assume its a
typo.

You want string slicing.

base = 'a0000'
extra = 'ab'
print( base[:-len(extra)] + extra )



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list