startswith(): checking if a string begins with a certain value in Python

Use the startswith() method to check if a string begins with a certain value.

phrase = "This is a regular text"

print(phrase.startswith('This is'))
#output: True

print(phrase.startswith('text'))
#output: False

You can also set if you want to begin the match in a specific position and end it in another specific position of the string.

phrase = "This is a regular text"

print(phrase.startswith('regular', 10)) #the word regular starts at position 10 of the phrase
#output: True

print(phrase.startswith('regular', 10, 22)) #look for in 'regular text'
#output: True

print(phrase.startswith('regular', 10, 15)) ##look for in 'regul'
#output: False

Finally, you might want to check for multiple strings at once, instead of using some kind of loop, you can use a tuple as an argument with all the strings you want to match against.

phrase = "This is a regular text"

print(phrase.startswith(('regular', 'This')))
#output: True

print(phrase.startswith(('regular', 'text')))
#output: False

print(phrase.startswith(('regular', 'text'), 10, 22)) #look for in 'regular text'
#output: True