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 and str.endswith should accept multiple arguments.
Type: enhancement Stage: resolved
Components: Interpreter Core, Unicode Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Elizacat, ethan.furman, ezio.melotti, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2015-04-24 05:49 by Elizacat, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg241912 - (view) Author: Elizabeth Myers (Elizacat) * Date: 2015-04-24 05:49
str.startswith and str.endswith should accept multiple arguments when passing in strings. This makes it easier to check if the first character of a string is one of a given option, versus this awkward construction:

>>> f = 'abc'
>>> 'test'.startswith(tuple(f))
False

With my proposal, this could be shortened to 'test'.startswith(*f) for easy testing.

This also makes it easier to check if a string begins with one of any combination of matches.
msg241915 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2015-04-24 06:03
We can already do

--> some_string.starts_with(('innie','minnie', 'minie', 'moe'))

Your proposal appears to be equivalent to:

--> 'test'.startswith(('a', 'b', 'c'))

How often do you check to see if a string starts with only a single character?

-1

tuple() is the correct solution to this problem.
msg241918 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-04-24 07:09
This proposition conflicts with optional parameters of str.startswith().

>>> 'test'.startswith(('a', 'b', 'c'), 1, 3)
False
History
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68235
2015-04-24 07:09:58serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg241918

resolution: rejected
stage: resolved
2015-04-24 06:03:42ethan.furmansetnosy: + ethan.furman
messages: + msg241915
2015-04-24 05:49:07Elizacatcreate