Check if the string contains a substring at the end

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

eg

text = 'lord_of_pizzas_DFG' if('DFG' in text): print('You shall pass') elif('DFG' not in text): print('You shall not pass') 

The problem is that if text='DFG_pizza_man' , it will still print "You will pass." I want to know how to check if DFG is at the end of a line.

+5
source share
1 answer

Use str.endswith

 text = 'lord_of_pizzas_DFG' if text.endswith("DFG"): print('You shall pass') else: print('You shall not pass') 
+18
source

Source: https://habr.com/ru/post/1203535/


All Articles