How to replace the last (and only last) character in a string?

Dave Borne dborne at gmail.com
Thu May 3 16:30:09 EDT 2007


> Let's suppose
> s='12345 4343 454'
> How can I replace the last '4' character?

If the last '4' will not always be the last character in the string,
you could do:
'X'.join(s.rsplit('4',1))

-Dave



More information about the Python-list mailing list