Passing Tuples to startswith

Sep 13, 2023

Here's another thing that linting taught me recently: Python's str.startswith() method, and str.endswith() as well, takes a tuple as the first parameter! This makes checking for multiple options really simple:

# Verbose way of writing it
if mystring.startswith('c.') or mystring.startswith('m.') or mystring.startswith('s.'):
    ...

# Easier way
if mystring.startswith(('c.', 'm.', 's.')):
   ...

I didn't realize the language allowed this!

No comments (yet!)