检查字符串末尾是否包含子字符串

2024-06-01 07:26:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检查变量末尾是否包含子字符串。

例如:

text = 'lord_of_pizzas_DFG'

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

我想知道如何检查“DFG”是否在字符串的末尾。我应该写什么而不是???来让代码打印“You shall pass”?


Tags: of字符串代码textyouifnotpass
1条回答
网友
1楼 · 发布于 2024-06-01 07:26:00

使用^{}

text = 'lord_of_pizzas_DFG'

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

相关问题 更多 >