Search code examples
pythonlisttextstartswith

text.startswith method with multi words


Hello I want to use the .startswith method, but I could only get it to work with one word.
I want more than one word.

For example what I did:

if text.startswith('welc')
   print('Welcome')

but I wanted:

list = ['welc', 'hey', 'sto']

if text.startswith(list)
   print('It works')
# This doesn't work

Solution

  • You can use any():

    >>> s = "welcome"
    >>> l = ['welc', 'hey', 'sto']
    >>> any(s.startswith(i) for i in l)
    True