May-30-2019, 03:56 PM
Before I get going, I know strings aren't mutable, which is why I am perplexed. I was writing myself a simple example, to prove that strings aren't mutable, and I can't explain my results.
I would have expected the 2nd and third strings to have different memory address, but they do not. It would indicate that the string was not copied, but changed.
Can anyone help explain this discrepancy?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 |
alphabet = "bcdefghijklmnopqrstuvwxy" print ( hex ( id (alphabet))) #memory address 'abc' print (alphabet) print ( '\nalphabet = "a" + alphabet:' ) alphabet = "a" + alphabet print ( hex ( id (alphabet))) #memory address 'xyz' print (alphabet) print ( '\nalphabet += z:' ) alphabet + = "z" print ( hex ( id (alphabet))) #memory address 'xyz' print (alphabet) |
Can anyone help explain this discrepancy?
Thanks!