Just when I though I knew enough about python operators!
Can someone explain why e is f false?
a = 'Goodbye'
b = 'Goodbye'
c = 'Good_Bye'
d = 'Good_Bye'
e = 'Good-Bye'
f = 'Good-Bye'
a is b
Out[9]: True
c is d
Out[10]: True
e is f
Out[11]: False
is checks for identity of two objects.
The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.
For immutable (e.g. str) literals, equal values can (incidentally) actual rely on the same object underpinning them, but that is not guaranteed or intentional -> you should not rely on that (emphasis added):
Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation.
is operator, but about objects of specific str literals. And for that as doc says, they "may or may not" be identical for the equal value thereof. It's for the interpret to decide, but the most important part is, it is a behavior you must not rely on, because while it may work one way in a specific interpreter of a specific version on your machine, it may not work the same way anywhere and everywhere else.
"-"specifically, and if it does, it's totally an internal detail you've stumbled on that is basically meaningless to reason about as a user of the CPython interpreter and may well change in future (or past) versions. Please read the entire dupe chain thoroughly--doingstring is some_other_stringis fundamentally incorrect and unpredictable.