Asked • 06/08/19

Why does comparing strings using either '==' or 'is' sometimes produce a different result?

I've got a Python program where two variables are set to the value `'public'`. In a conditional expression I have the comparison `var1 is var2` which fails, but if I change it to `var1 == var2` it returns `True`.Now if I open my Python interpreter and do the same "is" comparison, it succeeds. >>> s1 = 'public' >>> s2 = 'public' >>> s2 is s1 TrueWhat am I missing here?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.