Ice_giant
Ice_giant

Reputation: 181

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"?

Upvotes: 11

Views: 22059

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180540

Use str.endswith

text = 'lord_of_pizzas_DFG'

if text.endswith("DFG"):
    print('You shall pass')
else:
    print('You shall not pass')

Upvotes: 38

Related Questions