Checking whether a string starts with XXXX

John Marston picture John Marston · Jan 10, 2012 · Viewed 365.9k times · Source

I would like to know how to check whether a string starts with "hello" in Python.

In Bash I usually do:

if [[ "$string" =~ ^hello ]]; then
 do something here
fi

How do I achieve the same in Python?

Answer

RanRag picture RanRag · Jan 10, 2012
aString = "hello world"
aString.startswith("hello")

More info about startswith.