0

When i am trying to compare the identity of two variable containing same string, python gives me different result.

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

print( msg is new_msg )
> False

But,

msg = "hello_world"
new_msg = "hello_world"

print( msg is new_msg )
> True
Mahmudul Alam
  • 143
  • 1
  • 8
  • 1
    Python doesn't promise to be consistent here, because the identity of a string should never matter. If it mattered, there would be a consistent rule. – user2357112 Apr 04 '20 at 09:39
  • 1
    "Why Python “is” operator gives different result for variable containing same string" Why do you expect those objects to have the same identity? – juanpa.arrivillaga Apr 04 '20 at 11:31
  • For your implementation of Python, [this answer](https://stackoverflow.com/a/23556366) seems to be the cause. – Jongware Apr 04 '20 at 11:48

0 Answers0