0

I know that == checks the values of the operands, and is checks the reference to the same object. However, I can't seem to figure out why the following codes behave differently.

Code1:

i=input()
  #Let's say input is Hello
i is "Hello" #Returns False

Code2:

i="Hello"
i is "Hello" #Returns True

Why are the outputs in the two cases different? Isn't assigning "Hello" same as inputting "Hello"?

martineau
  • 119,623
  • 25
  • 170
  • 301
Soul_Hacker
  • 93
  • 1
  • 10
  • Also see the [following post about string interning](https://stackoverflow.com/questions/15541404/python-string-interning) – Cory Kramer Jun 02 '17 at 12:19
  • Also see [Understanding Python's "is" operator](//stackoverflow.com/q/13650293) – Martijn Pieters Jun 02 '17 at 12:20
  • Your second example returns true for the reasons outlined in the duplicate post. It is an implementation detail, strings that look like valid Python identifiers happen to be interned for performance reasons. – Martijn Pieters Jun 02 '17 at 12:21

0 Answers0