This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str.startswith/endswith could take a tuple?
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, rhettinger, tlynn
Priority: normal Keywords:

Created on 2006-05-19 10:24 by tlynn, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg54805 - (view) Author: Tom Lynn (tlynn) Date: 2006-05-19 10:24
In the same way that exceptions can have a tuple of 
types specified and isinstance can take a tuple of 
types, str.startswith and endswith could take a tuple 
of possible prefixes/suffixes.

One use-case::
    if filename.endswith(('jpg', 'gif', 'png')):
         # ...
vs:
    if (filename.endswith('jpg') or filename.endswith
('gif') or
        filename.endswith('png')):
        #...

Obviously it's not a huge improvement in clarity, but 
it does seem to be an improvement to me.
msg54806 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-05-19 20:58
Logged In: YES 
user_id=80475

FWIW, in Py2.5, this would be written:

   if any(filename.startswith(s) for s in suffixes):
      . . .
msg54807 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-06-05 16:33
Logged In: YES 
user_id=80475

The previous comment was just information.  I am +1 on the
proposal because the use case is so common and the proposed
form is concise, clear, and natural.
msg54808 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-06-06 00:30
Logged In: YES 
user_id=80475

Georg, would you like to implement this one?
msg54809 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-06-07 21:11
Logged In: YES 
user_id=849994

I will, tomorrow.
msg54810 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-06-09 20:23
Logged In: YES 
user_id=849994

Committed implementation in rev. 46795.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43378
2006-05-19 10:24:24tlynncreate