Archive
Posts Tagged ‘endswith’
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 ‘avi‘ or ‘mp4‘, 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
endswith, startswith, tip, tuple
