Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)
recategorized by

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?

1 Answer

0 votes
by (106k points)

To check whether a string starts with XXXX you can use startswith() function below is the code that illustrates the use of startswith() function:-

startswith():-

Syntax of function is str.startswith(search_string, start, end) where the first argument is the string to be searched, second and third arguments are optional in case you want to search the string from any fixed index then you need to put the second and third argument in the function.

abc_string = "hello world"

abc_string.startswith("hello")

image

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...