【发布时间】:2020-05-19 09:43:57
【问题描述】:
我正在尝试检查一行中是否有机器人一词。 (我正在学习 python。对它还是很陌生。)如果“机器人”这个词在一行中,它会打印出一些东西。与“机器人”相同。但是,我需要知道当机器人在线但在随机混合情况下如何输出,例如 rObOt。这可能吗?看来我需要写出每个组合。我正在使用 Python 3。谢谢 :)。
if ' robot ' in line:
print("There is a small robot in the line.")
elif ' ROBOT ' in line:
print("There is a big robot in the line.")
elif 'rOBOt' in line:
print("There is a medium sized robot in the line.")
else:
print("No robots here.")
【问题讨论】:
-
使用
lower()将整行转换为小写,然后检查robot。找出robot是否是一个独立的词有点困难,最好使用带有词边界的正则表达式来解决。 -
.lower()两者并进行比较。
标签: python string lowercase capitalization mixed-case