+ 2

I don't know why the ouput is "True". Help me please!

The output of this code is "True": a = '''text''' b = 'text' print(a is b). Can you explain to me? Thanks for reading.

5th Jan 2022, 2:54 PM
Phúc
2 odpowiedzi
+ 7
Phúc , in this case python shares the data (in memory) "text" for both variables *a* and *b*. we can check this with using the built-in function id(): ... print(id(a), id(b)) when we add this line to your code, the output for *a* and. *b* will be the same. if we now modify one of the variables like: ... b += "!" print(id(a), id(b)) the id's of the variables are no longer the same ... print(a is b) will give *False* now
5th Jan 2022, 3:14 PM
Lothar
Lothar - avatar
+ 1
Lothar Thank you so much
5th Jan 2022, 3:22 PM
Phúc