【发布时间】:2020-04-20 16:29:16
【问题描述】:
我对编码很陌生,但我虽然了解“和”运算符的工作原理。在我提供的示例中,我会认为“假”语句会运行而不是“真”语句。有人能告诉我为什么这没有像我预期的那样表现吗?
string = 'asdf'
if 'z' and 's' in string:
True
else:
False
【问题讨论】:
-
你想要
'z' in string and 's' in string -
如果您有两个以上的字符,请使用列表理解:
if all(c in string for c in 'zs'): -
@Boris 我什至会为两个字符这样做以保留代码dry
标签: python-3.x expression comparison-operators