I know that strings are immutable in Python. However, the following experiment puzzles me.
First, we create a string using the str() function passing it some other object (in our case, dictionary):
>>> a = {1: 100, 2: 200}
>>> b = str(a)
>>> b
'{1: 100, 2: 200}'
Then we check its ID:
>>> id(b)
111447696
Then we "modify" the string:
>>> b = b + ' f'
>>> b
'{1: 100, 2: 200} f'
And then we check the ID of the "modified" string:
>>> id(b)
111447696
I would expect that IDs of b
would be different before and after the modification, since strings are immutable, so adding ' f'
to a string would produce another string, with a different ID, referring to a different location in memory. How come two different strings have the same ID?