[英]Why does str.lower() modify modify string comp
I have the following python code snipped and have been getting insane as i don't understand why I'm getting these different results.我有以下 python 代码被剪断并且变得疯狂,因为我不明白为什么我会得到这些不同的结果。 Can anyone elaborate?
谁能详细说明?
test = "GTX1050Ti 4GB"
print(test)
if "gtx" and "560" and "ti" in test:
print("GTX 560 Ti")
else:
print("nope")
print(test.lower())
if "gtx" and "560" and "ti" in test.lower():
print("GTX 560 Ti")
else:
print("nope")```
Output:
GTX1050Ti 4GB
nope
gtx1050ti 4gb
GTX 560 Ti
You can use all
to help ensure the condition is met for all checks您可以使用
all
来帮助确保所有检查都满足条件
checks = ['gtx', '560', 'ti']
if all(check in test for check in checks):
....
if all(check in test.lower() for check in checks):
....
Then if you need to change what is checked, you only need to do it in once place.然后,如果您需要更改检查的内容,您只需要在一次地方进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.