user997225
user997225

Reputation:

Shortest way of checking whether a string starts with a specified set of substrings

I have a string color_line which I need to check whether it starts with a substring which takes values only in 'red', 'blue', 'green', or 'magenta'.

Is there a shorter way of doing the check than the obvious way of

if line.startswith( 'red' ) or ... or line.startswith( 'magenta' ): ...

Upvotes: 0

Views: 62

Answers (1)

user2555451
user2555451

Reputation:

You can pass a tuple of values to str.startswith:

if line.startswith(('red', 'blue', 'green', 'magenta')):

Upvotes: 5

Related Questions