Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
1 view
by (19k points)

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
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...