首页 > 解决方案 > 为什么 Python "is" 运算符对包含相同字符串的变量给出不同的结果

问题描述

当我试图比较包含相同字符串的两个变量的身份时,python 给了我不同的结果。

msg = "hello world"
new_msg = "hello world"

print( msg is new_msg )
> False

但,

msg = "hello_world"
new_msg = "hello_world"

print( msg is new_msg )
> True

标签: pythonpython-3.xoperator-keywordidentity-operator

解决方案


推荐阅读