Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contains substring at the end

I would like to check if a variable contains a substring at the end.

For example:

text = 'lord_of_pizzas_DFG'

if ???:
    print('You shall pass')
else:
    print('You shall not pass')

I want to know how to check if "DFG" is at the end of the string. What do I write instead of ??? to make the code print "You shall pass"?

like image 782
Ice_giant Avatar asked Sep 27 '14 10:09

Ice_giant


1 Answers

Use str.endswith

text = 'lord_of_pizzas_DFG'

if text.endswith("DFG"):
    print('You shall pass')
else:
    print('You shall not pass')
like image 66
Padraic Cunningham Avatar answered Oct 22 '22 16:10

Padraic Cunningham