Why does comparing strings using either '==' or 'is' sometimes produce a different result in Python?
Last Updated :
09 Jan, 2023
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.
Similar Reads
Using Else Conditional Statement With For loop in Python Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while
2 min read
Compare Strings for equality or lexicographical order in different programming Languages Given two strings string1 and string2, the task is to check if these two strings are equal or not. Examples: Input: string1 = âGeeksforGeeksâ, string2 = âGeeksforGeeksâ Output: Yes Input: string1 = âGeeks for Geeksâ, string2 = âGeeks for Geeksâ Output: Yes Input: string1 = âGeeksforGeeksâ, string2 =
8 min read
Return a boolean array which is True where the string element in array ends with suffix in Python In this article, we are going to see how we will return a boolean array which is True where the string element in the array ends with a suffix in Python. numpy.char.endswith() numpy.char.endswith() return True if the elements end with the given substring otherwise it will return False. Syntax : np.c
2 min read
Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx
8 min read
Difference between == and is operator in Python In Python, == and is operators are both used for comparison but they serve different purposes. The == operator checks for equality of values which means it evaluates whether the values of two objects are the same. On the other hand, is operator checks for identity, meaning it determines whether two
4 min read