endswith(): check if a string ends with a certain value in Python

Use the endswith() method to check if a string ends with a certain value.

phrase = "This is a regular text"

print(phrase.endswith('regular text'))
#output: True

print(phrase.endswith('This'))
#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"

#look for in 'This is', the rest of the phrase is not included
print(phrase.endswith('This is', 0, 7))
#output: True

#look for in 'This is a regular'
print(phrase.endswith('regular', 0, 17)) 
#output: True

#look for in 'This is a regul'
print(phrase.endswith('regular', 0, 15)) 
#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.endswith(('regular', 'This', 'text')))
#output: True

print(phrase.endswith(('regular', 'is')))
#output: False

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