Open In App

Why does comparing strings using either ‘==’ or ‘is’ sometimes produce a different result in Python?

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python offers many ways of checking relations between two objects. Unlike some high-level languages, such as C++, Java, etc., which strictly use conditional operators. The most popular operators for checking equivalence between two operands are the == operator and the is operator. But there exists a difference between the two operators. In this article, you will learn why comparing strings using ‘==’ or ‘is’ sometimes produces different results in Python.

What is the ‘is’ operator?

is is an identity operator in Python. That is used to check if two values (or variables) are located on the same part of the memory. Two equal variables do not imply that they are identical. i.e., The operators compare the id of both the operands, and if they are the same, then the output is true otherwise false. An example to demonstrate the use case of is operator is:

Python3




a = "apples"
b = "apples"
c = "". join(['a', 'p', 'p', 'l', 'e', 's'])
  
print(a is b)
print(a is c)


Output:

True
False

Firstly two variables were initialized with the same string (interning). Then a list containing the same alphabet as the string mentioned earlier was defined and joined using the join function. Then a and b are checked for identity equivalence, and a and c are also checked for identity equivalence. The former results in True as both the strings are simultaneously existing objects. Python only created one string object, and both a and b refer to it. The reason is that Python internally caches and reuses some strings as an optimization. The latter results in a False even though the contents of either string are the same. This is because the c string was created by joining a list. Due to this, the list was the initial object whose id did not equal that of the string. Therefore, even though the contents of the strings are the same, the result is False.

What is the ‘==’ operator?

== is a conditional/relational operator used to check whether the operands are identical. The operator considers the data stored by the operands to check their equality. An example to demonstrate the use case of the == operator is:

Python3




a = "apples"
b = "apples"
c = "". join(['a', 'p', 'p', 'l', 'e', 's'])
  
print(a == b)
print(a == c)


Output:

True
True

The same code as the previous one was used, this time with the == operator for checking. Both the checks evaluated to True result. This is because the operator checks the contents of the operands for comparison, which are the same in both cases. Therefore, it is advised to use the == operator over is operator if wanting to check whether the operands are equivalent.

When to use the ‘is’ operator and when to use the == operator?

Both operators, even though offering similar functionality, should be used for completely different purposes. The is operator checks whether the two operands are identical or not. By identical, it means whether the two objects are the same. It does so by checking the id of both objects, and if the id is the same, then the objects are true (not in the case of numbers). Comparison of integers and strings (for equality) should never be done using the operator. Comparisons to singletons like None should be done using the operator. 

Hence, the is operator should be used to check identity, not equivalence.

The == operator is used to check the equivalence between the operands. The operator does not require the operands to be identical. The operator should be used to check the equality between integers and strings

Hence, the == operator should be used to check equivalence, not identity. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads