Archive

Posts Tagged ‘startswith’

endswith also accepts a tuple

November 7, 2015 1 comment

Have you ever written something like this?

fname = 'movie.avi'
if fname.endswith('avi') or fname.endswith('mp4'):
    print("It's a movie.")

The function endswith also accepts a tuple. Just saying.

fname = 'movie.avi'
if fname.endswith(('avi', 'mp4')):
    print("It's a movie.")

Meaning: if it ends as ‘aviormp4‘, then…

This also works with startswith, of course.

Thanks to one of my students, Marton Sz. who solved one of his exercises using this trick.

Categories: python Tags: , , ,