![](/img/trans.png)
[英]How to check if two elements at the same number index in different strings match?
[英]How to check if two strings are the same
伙計們
愚蠢的問題,但一個使我困惑的問題
print test
print test == "None"
在終端上打印出來
None
False
如您所見,test為None,但檢查它們是否相同的檢查返回false
為什么是這樣 ?
嘗試這個:
print test
print test == "None"
print test == None
print type(test)
我認為您"None"
將字符串"None"
分配給test
。 test
很可能是NoneType
因此您應該像這樣測試它:
print test is None
結果與test == None
相同,但是PEP8表示您應該使用關鍵字is
代替==
來測試單例對象的相等性,例如None
。
在python中, None
是數據類型,因此在可以有數字或字符串的地方也可以有None。
對於您的情況,要檢查它是否為None
只需刪除引號即可:
print test == None
但是您掌握了測試兩個字符串是否相同的原則:
test = "Hello"
print test
print test == "Hello"
得到:
Hello
True
這是因為None
不是字符串,它的類型為NoneType
。 將其與其他語言“ null”進行比較。 與值的字符串"None"
是不一樣的None
。
也許文檔可以為您提供更多幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.