2
 >>>name = "lucy"
 >>>name is "lucy"
 >>>True
 >>>name=input()
 lucy
 >>>name is "lucy"
 False

I know that is checks for object identity. But isn't the object identity of name and "lucy" same here?

Also, why does assigning "lucy" to name results as True, whereas entering "lucy" as input and then assigning it to name results in False?

Aamir Khan
  • 324
  • 2
  • 10
  • 2
    It is likely due to [string interning](http://guilload.com/python-string-interning/), for example [read this](https://stackoverflow.com/questions/15541404/python-string-interning) – Cory Kramer May 17 '17 at 11:22
  • I also had used single quotes. I'm still getting the same result. Version of Python is 3.5.3 – Aamir Khan May 17 '17 at 11:26
  • 3
    Here you have a couple of questions that might answer yours: [Python Interpreter String Pooling Optimization](http://stackoverflow.com/questions/42364044/python-interpreter-string-pooling-optimization), [When does python choose to intern a string](http://stackoverflow.com/questions/10622472/when-does-python-choose-to-intern-a-string) – Carles Mitjans May 17 '17 at 11:26
  • I'm double checking, when copy paste exactly this then get False; but when do this: >>> n = 'a' >>> n is 'a' True >>> n=input() a >>> n is 'a' True – Drako May 17 '17 at 11:27
  • @CoryKramer There is no link attached in your comment. Could you please send the link of whatever you'd like me to read? – Aamir Khan May 17 '17 at 11:28
  • Have really no clue, but when use variable name of 1 letter it results in true, when variable name longer in False ` >>> var = 'abc' >>> var is 'abc' True >>> var = input() abc >>> var is 'abc' False >>> w='n' >>> w is 'n' True >>> w=input() n >>> w is 'n' True >>>` – Drako May 17 '17 at 11:33
  • @Drako Same here. Going through the links provided by CoryKramer. I hope I'll be able to come up with an answer after reading. :) – Aamir Khan May 17 '17 at 11:37
  • @CarlesMitjans I think your comment can go as answer here :) That makes sense and at same time no; now I'm scared to compare strings :) – Drako May 17 '17 at 11:37

0 Answers0