簡體   English   中英

為什么str.lower() modify 修改字符串comp

[英]Why does str.lower() modify modify string comp

我有以下 python 代碼被剪斷並且變得瘋狂,因為我不明白為什么我會得到這些不同的結果。 誰能詳細說明?

   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

您可以使用all來幫助確保所有檢查都滿足條件

checks = ['gtx', '560', 'ti']
if all(check in test for check in checks):
    ....

if all(check in test.lower() for check in checks):
    ....

然后,如果您需要更改檢查的內容,您只需要在一次地方進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM