简体   繁体   English

python-多种可能性的“ if in”语句

[英]python - “if in” statement for multiple possibilities

I want to check if a question mark or a dot are in the last position of a word. 我想检查问号或圆点是否在单词的最后位置。 So doing something like: 所以做这样的事情:

if ".","?" in word[-1]:

Or: 要么:

if [".","?"] in word[-1]:

Result in an invalid syntax. 结果语法无效。 What is the right way to do this? 什么是正确的方法? (preferably without regex) (最好不使用正则表达式)

对于这种特殊情况,您要检查结尾:

if word.endswith(('.','?')):

You got the order backwards. 你把订单倒了。

if word[-1] in ".?":

Refer to the in operator in the documentation 请参考文档中in运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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