Prev: inflect.py: generate plurals, ordinals, numbers towords...
Next: Is '[' a function or an operator or an language feature?
From: python on 18 Jul 2010 21:51 And don't forget the oft requested strip_tease(). Malcolm
From: News123 on 19 Jul 2010 03:06
Dennis Lee Bieber wrote: > On Sun, 18 Jul 2010 17:49:11 +0100, MRAB <python(a)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 |