So, let's go through what we know:
- The
is
operator compares identity, not value, as opposed to the==
operator. - Python interns string literals, so
"hello" is "hello"
isTrue
.
So what I don't understand is this behavior:
>>>'h' is input()
h
True
>>>'hj' is input()
hj
False
A single character is counted as identical, even though they're not both string literals, whereas a two-char string gives me the results I expect, of non-identicalness.
Since the input() function is creating a string dynamically, the result is not being interned, which is why 'hj'
and dynamically created 'hj'
are not identical. But why are 'h'
and dynamic 'h'
identical?
Does this mean Python caches/interns all strings of length 1?