Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Startswith(): more arguments

Tags:

python

string

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! :)

like image 885
MarkF6 Avatar asked Dec 19 '22 11:12

MarkF6


1 Answers

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
like image 79
falsetru Avatar answered Jan 02 '23 16:01

falsetru