MarkF6
MarkF6

Reputation: 503

Startswith(): more arguments

is there a function in python to check whether a string starts with a certain number of possibilities? For example, I'd like to check whether string A starts with "Ha", "Ho", "Hi". I thought I could use string_A.startswith("Ha", "Ho", "Hi"), but unfortunately, this is not possible :(

Thanks for any advice! :)

Upvotes: 4

Views: 6107

Answers (1)

falsetru
falsetru

Reputation: 369134

You need to pass them as a single tuple:

>>> "Hi, there".startswith(('Ha', 'Ho', 'Hi'))
True
>>> "Hoy, there".startswith(('Ha', 'Ho', 'Hi'))
True
>>> "Hello, there".startswith(('Ha', 'Ho', 'Hi'))
False

Upvotes: 13

Related Questions