rstrip()

News123 news1234 at free.fr
Mon Jul 19 03:06:09 EDT 2010


Dennis Lee Bieber wrote:
> On Sun, 18 Jul 2010 17:49:11 +0100, MRAB <python at mrabarnett.plus.com>
> declaimed the following in gmane.comp.python.general:
> 
>> How about 'strip_str', 'lstrip_str' and 'rstrip_str', or something
> 
> 	Not sure what the first would do... unless one is envisioning
> 
> 	"abracadabra".strip_str("abra")...
> 
> 	For the others... trim_suffix, trim_prefix are more explicit in
> meaning -- after all, someone might ask if there is an *strip_unicode
> too! (or _bytes). That still leaves the question of what the desired
> behavior of
> 
> 	"Shanana".trim_suffix("na") 
> 
> is to produce? "Sha" or "Shana"?
> 
> 


What I'd imagine would be stripping of a suffix or the first match in a
list of suffixes exactly once.

TO me this seems the most common use case, though one is surprised how
functions can be used.


Example:

def strip_suffix(str,*suffixes):
    for suffix in suffixes:
        if str.endswith(suffix):
            return str[-len(suffix):]
    return str



More information about the Python-list mailing list