Python string with space and without space at the end and immutability
I learnt that in some immutable classes, __new__ may return an existing instance - this is what the int , str and tuple types sometimes do for small values. But why do the following two snippets differ in the behavior? With a space at the end: >>> a = 'string ' >>> b = 'string ' >>> a is b False Without a space: >>> c = 'string' >>> d = 'string' >>> c is d True Why does the space bring the difference? This is a quirk of how the CPython implementation chooses to cache string literals. String literals with the same contents may refer to the same string object, but they don't have to. 'string'