Skip to content Skip to sidebar Skip to footer

Compare Two Group String,return Different Results

I run this command in python console: why the 2 results are different? >>>S1 = 'HelloWorld' >>>S2 = 'HelloWorld' >>>S1 is S2 True >>>S1 = 'Hello

Solution 1:

is the result will be true only if the object is the same object.

== will be true if the values of the object are the same.

>>> S1 = 'HelloWorld'
>>> print id(S1)
4457604464
>>> S2 = 'HelloWorld'
>>> print id(S2)
4457604464
>>> S1 is S2
True

The above code means S1 and S2 are same object.And they have the same memory location.So S1 is S2.

>>> S1 = 'Hello World'
>>> S2 = 'Hello World'
>>> print id(S1)
4457604320
>>> print id(S2)
4457604272
>>> S1 is S2
False

Now,they're different object,so S1 is not S2.


Post a Comment for "Compare Two Group String,return Different Results"