Checking whether a string starts with XXXX Checking whether a string starts with XXXX python python

Checking whether a string starts with XXXX


aString = "hello world"aString.startswith("hello")

More info about startswith.


RanRag has already answered it for your specific question.

However, more generally, what you are doing with

if [[ "$string" =~ ^hello ]]

is a regex match. To do the same in Python, you would do:

import reif re.match(r'^hello', somestring):    # do stuff

Obviously, in this case, somestring.startswith('hello') is better.


In case you want to match multiple words to your magic word, you can pass the words to match as a tuple:

>>> magicWord = 'zzzTest'>>> magicWord.startswith(('zzz', 'yyy', 'rrr'))True

startswith takes a string or a tuple of strings.


matomo