What is the mechanism of + = operation and = + operation of a string?

-sharp a
>>> a = "s2@"
>>> id(a)
49220672
>>> a = a + "s"
>>> id(a)
49220672
>>> a += "@"
>>> id(a)
49220672
-sharp a
>>> a = "-sharp$@-sharp$@"
>>> id(a)
49220544
>>> a += "23"
>>> id(a)
49302888
>>> a = a + "sd"
>>> id(a)
49302888
-sharp a
>>> a = "@-sharp$-sharp@%"
>>> id(a)
49220608
>>> a += "^%&%&"
>>> id(a)
49302728
>>> a = a + "&^**%$^"
>>> id(a)
49296880

what causes three different results.

Oct.21,2021

the reason for this is the pooling mechanism in python. Simple and commonly used shorter strings are stored in memory and memory pooling technology is used to improve performance.

For more information, please refer to the answer understands pooling in python

.
Menu