Why does comparing strings using either '==' or 'is' sometimes produce a different result?
is is identity testing,
== is equality testing. what happens in your code would be emulated in the interpreter like this:
'pub'b = ''.join(['p', 'u', 'b']) a == bTruea is bFalsea =
so, no wonder they're not the same, right?
In other words:
a is b is the equivalent of
id(a) == id(b)
Other answers here are correct:
is is used for identity comparison, while
== is used for equality comparison. Since what you care about is equality (the two strings should contain the same characters), in this case the
is operator is simply wrong and you should be using
is works interactively is that (most) string literals are interned by default. From Wikipedia:
Interned strings speed up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on hash tables with string keys. Without interning, checking that two different strings are equal involves examining every character of both strings. This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fills up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.
So, when you have two string literals (words that are literally typed into your program source code, surrounded by quotation marks) in your program that have the same value, the Python compiler will automatically intern the strings, making them both stored at the same memory location. (Note that this doesn't always happen, and the rules for when this happens are quite convoluted, so please don't rely on this behavior in production code!)
Since in your interactive session both strings are actually stored in the same memory location, they have the same identity, so the
is operator works as expected. But if you construct a string by some other method (even if that string contains exactly the same characters), then the string may be equal, but it is not the same string -- that is, it has a different identity, because it is stored in a different place in memory.