feature request: a better str.endswith

Skip Montanaro skip at pobox.com
Fri Jul 18 15:23:31 CEST 2003

    Michele> I often feel the need to extend the string method ".endswith"
    Michele> to tuple arguments, in such a way to automatically check for
    Michele> multiple endings.  For instance, here is a typical use case:

    Michele> if filename.endswith(('.jpg','.jpeg','.gif','.png')):
    Michele>     print "This is a valid image file"

This is analogous to how isinstance works, where its second arg can be a
class or type or a tuple containing classes and types.

I suggest you submit a feature request to SF.  A patch to stringobject.c and
unicodeobject.c would help improve chances of acceptance, and for symmetry
you should probably also modify the startswith methods of both types.


More information about the Python-list mailing list