I can't seem to be able to create two strings that have the same value but different identities. I'm using PyCharm 2023.1 and I'm very new to Python, I've just learned about the difference between == and is operators and I want to actually see the difference for myself but no matter what I try Python just interns my strings and returns True!
I should say that I'm aware that this feature is there for optimization purposes and I shouldn't rely on it to always use is instead of ==. I really just want to see the difference for myself.
Here's my original code:
firststr= 'blah blah blah'
print(id(firststr))
secondstr= 'blah blah blah'
print(id(secondstr))
print(firststr is secondstr)
And here's the result I got:
2768763221232
2768763221232
True
First I googled the problem and found this: https://www.jetbrains.com/help/pycharm/variables-loading-policy.html I thought maybe changing the variable loading policy would help. I thought maybe because it's set on Asynchronous and it creates objects one by one it just gives new objects that have the same value as a previously created one the same id and set it so Synchronous but nothing changed!
I asked ChatGPT twice and got two different answers:
firststr = 'blah blah blah'
print(id(firststr ))
secondstr= str('blah blah blah' + '')
print(id(secondstr))
print(firststr is secondstr)
and
firststr = 'blah blah blah'
print(id(firststr))
secondstr = 'blah blah blah'[:]
print(id(secondstr))
print(firststr is secondstr)
Neither of them worked and I still got the same results.
I found an old post on Stackoverflow that recommended using the Copy() method:
import copy
firststr = 'blah blah blah'
print(id(firststr ))
secondstr= copy.copy(firststr)
print(id(secondstr))
print(firststr is secondstr)
and
import copy
firststr = 'blah blah blah'
print(id(firststr ))
secondstr= copy.deepcopy(firststr)
print(id(secondstr))
print(firststr is secondstr)
But AGAIN, neither of them worked and I got the same results.
Does anyone know how I'm actually supposed to create two strings with the same value but different identities?