Can someone explain this to me? So I\'ve been playing with the id() command in python and came across this:
>>> id(\'cat\')
5181152
>>> a = \'c
All values must reside somewhere in memory. This is why id('cat')
produces a value. You call it a "non-existent" string, but it clearly does exist, it just hasn't been assigned to a name yet.
Strings are immutable, so the interpreter can do clever things like make all instances of the literal 'cat'
be the same object, so that id(a)
and id(b)
are the same.
Operating on strings will produce new strings. These may or may not be the same strings as previous strings with the same content.
Note that all of these details are implementations details of CPython, and they can change at any time. You don't need to be concerned with these issues in actual programs.