string.rstrip

Peter Hansen peter at engcorp.com
Tue Sep 23 18:04:26 EDT 2003


Matthew Wilson wrote:
> 
> What would be the best way to strip off the last "_asdf" from
> "asdf_asdf_asdf"?

s = "asdf_asdf_asdf"
without_last_asdf = s[:-len("_asdf")]

of course, that works only if you know it's there...  if not, 
prepending with a test for s.endswith("_asdf") would do the trick.

-Peter




More information about the Python-list mailing list