string.rstrip

Matthew Wilson mwilson at sarcastic-horse.com
Tue Sep 23 23:50:52 CEST 2003


What would be the best way to strip off the last "_asdf" from
"asdf_asdf_asdf"?


>>>>>> "Hank" == Hank  <soundwave56 at yahoo.ca> writes:
>
>     Hank> The last 't' was stripped. Is this fixed or documented
>     Hank> anywhere? it works for other cases, but this one seems to
>     Hank> give me a weird result.
>
> That's because rstrip removes any characters up until the first one
> one that doesn't match.  Since the 't' before the '.' is in the strip
> string '.txt' it is stripped too.  Does this example clarify:
>
>
>    >>> 'John D. Hunter'.rstrip('retD')
>    'John D. Hun'
>
> If you want to remove the extension, the best way is to use splitext
>
>   >>> import os
>   >>> base, ext  = os.path.splitext('test.txt')
>   >>> base
>   'test'
>
> Hope this helps,
> John Hunter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>






More information about the Python-list mailing list