I am trying to understand the working of id() function in Python for strings. If two objects have same string values, then their id is same.
Ex.1:
>>> alphabet1 = 'a'
>>> alphabet2 = 'a'
>>> id(alphabet1)
2046618561072
>>> id(alphabet2)
2046618561072
But, if strings are of multiple lengths, then id becomes different, even though equal strings are assigned to the objects.
Ex2.
>>> str1 = 'i am'
>>> str2 = 'i am'
>>> id(str1)
2046629110128
>>> id(str2)
2046628799536
Can anyone explain the reason/logic behind this or provide a useful link. I tried to refer this, but did not find the answer.
Thanks for any help !
Edit: As pointed out by @Epsi95, this is the case only if the multi-length strings have special symbols (and I also found when there are spaces as well, but except an underscore). So, a plain multi-length string without spaces and special symbols will give you same id.
Ex3.
>>> s1='asdfghjk44_l'
>>> s2='asdfghjk44_l'
>>> id(s1)
2046629110768
>>> id(s2)
2046629110768