[英]How to check if two strings are the same
H guys 伙计们
silly question but one that is confusing me 愚蠢的问题,但一个使我困惑的问题
print test
print test == "None"
in the terminal this prints out 在终端上打印出来
None
False
as you can see test is None, but the check to see if they are the same comes back false 如您所见,test为None,但检查它们是否相同的检查返回false
why is this ? 为什么是这样 ?
Try this: 尝试这个:
print test
print test == "None"
print test == None
print type(test)
I don't think that you assigned the string "None"
to test
. 我认为您
"None"
将字符串"None"
分配给test
。 It's likely that test
is a NoneType
so you should test it like this: test
很可能是NoneType
因此您应该像这样测试它:
print test is None
The result is the same as test == None
but the PEP8 says that you should use the keyword is
instead of ==
to test for equality of singleton objects, like None
. 结果与
test == None
相同,但是PEP8表示您应该使用关键字is
代替==
来测试单例对象的相等性,例如None
。
In python then None
is a data type, so where you can have a number, or a string, you can also have None. 在python中,
None
是数据类型,因此在可以有数字或字符串的地方也可以有None。
In your case, to check if it's None
just remove the quotes: 对于您的情况,要检查它是否为
None
只需删除引号即可:
print test == None
But you got the principle for testing if two strings are the same right: 但是您掌握了测试两个字符串是否相同的原则:
test = "Hello"
print test
print test == "Hello"
Gives: 得到:
Hello
True
It is because None
is not a string, it is of type NoneType
. 这是因为
None
不是字符串,它的类型为NoneType
。 Compare it to other languages 'null'. 将其与其他语言“ null”进行比较。 The string with value
"None"
is not the same as None
. 与值的字符串
"None"
是不一样的None
。
Perhaps the documentation can help you more. 也许文档可以为您提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.