简体   繁体   中英

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

Use str.endswith

text = 'lord_of_pizzas_DFG'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM