For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?

You use == when comparing values and is when comparing identities.

When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.

For boolean values, you shouldn't be doing comparisons at all. Instead of:

if x == True:
    # do something

write:

if x:
    # do something

For comparing against None, is None is preferred over == None.

I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

Yes, that's exactly what it's for.

Answer from dan04 on Stack Overflow
Top answer
1 of 4
679

For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?

You use == when comparing values and is when comparing identities.

When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.

For boolean values, you shouldn't be doing comparisons at all. Instead of:

if x == True:
    # do something

write:

if x:
    # do something

For comparing against None, is None is preferred over == None.

I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

Yes, that's exactly what it's for.

2 of 4
284

I would like to show a little example on how is and == are involved in immutable types. Try that:

a = 19998989890
b = 19998989889 +1
>>> a is b
False
>>> a == b
True

is compares two objects in memory, == compares their values. For example, you can see that small integers are cached by Python:

c = 1
b = 1
>>> b is c
True

You should use == when comparing values and is when comparing identities. (Also, from an English point of view, "equals" is different from "is".)

๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ string equals check in python: using 4 different methods
String Equals Check in Python: Using 4 Different Methods - AskPython
May 24, 2023 - The __eq__() method basically compares two objects and returns a boolean True or False. It returns True if strings are equivalent, otherwise, it returns False. ... str1 = "Python" str2 = "Python" str3 = "Java" if(str1.__eq__(str3)): print("str1 ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-comparison
Python Compare Strings - Methods & Best Practices | DigitalOcean
April 17, 2025 - You can compare strings in Python using the equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-equals
How to Check if Two Strings Are Equal in Python (With Examples) | DigitalOcean
September 12, 2025 - This operator checks if the value of the variable is equal to the string. ... Yes, you use the == operator for strings in Python.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-comparison-in-python
String Comparison in Python - GeeksforGeeks
The == operator is a simple way to check if two strings are identical. If both strings are equal, it returns True; otherwise, it returns False. ... s1 = "Python" s2 = "Python" # Since both strings are identical, therefore it is True print(s1 == s2)
Published ย  July 23, 2025
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - In python language, we can compare ... of the string comparison operators used for this purpose below: ==: This operator checks whether two strings are equal....
Find elsewhere
๐ŸŒ
Pythonspot
pythonspot.com โ€บ python-strings
python if string equals - Python Tutorial
This is the location of python ... index is always [0]. The last index is the length of the string minus one. ... The equality operator (==) tests if two variables have an equal value....
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ thinkcspy โ€บ Strings โ€บ StringComparison.html
9.8. String Comparison โ€” How to Think like a Computer Scientist: Interactive Edition
Strings are compared character by character. ... Yes, upper case is less than lower case according to the ordinal values of the characters. ... Python is case sensitive meaning that upper case and lower case characters are different.
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ python-string-comparison
Various methods for Python String Comparison - Flexiple Tutorials - Flexiple
The "==" is a python string comparison method that checks if both the values of the operands are equal.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - If they are equal, True is returned; otherwise, False is returned. print('abc' == 'abc') # True print('abc' == 'xyz') # False ... This operation is case-sensitive, as are other comparison operators and methods. Case-insensitive comparisons are discussed later. ... To check for partial matches, use the in operator, which determines if one string contains another string.
๐ŸŒ
Squash
squash.io โ€บ string-comparison-in-python-best-practices-and-techniques
String Comparison in Python: Best Practices and Techniques
1. Use Equality Comparison (==): For exact matches, the equality operator is the fastest. 2. Avoid Unnecessary Conversions: Minimize operations like .lower() unless needed. 3. Leverage String Interning: Python interns short strings, making comparisons faster.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ articles โ€บ compare-strings-in-python
Compare strings in Python
It means, if two variables are assigned the same string value, they are really referring to the same string object in memory. This fact can be verified by checking their id() value. ... Hence, comparison operator == for checking equality ...
๐ŸŒ
Cherry Servers
cherryservers.com โ€บ home โ€บ blog โ€บ cloud computing โ€บ how to do string comparison in python [with examples]
How to do String Comparison in Python | Cherry Servers
November 7, 2025 - While languages like C and C++ use ASCII codes for string comparison, Python uses Unicode values to compare characters. There are many occasions when we need to perform string comparisons.
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ 12 ways to compare strings in python with examples
12 Ways To Compare Strings In Python With Examples
March 18, 2024 - Equal: False Not Equal: True Less Than: True Greater Than: False Less Than or Equal: True Greater Than or Equal: False ... We begin the Python program above by declaring and initializing two integer variables, x and y, with the values of 10 and 15, respectively. Then, as mentioned in the code comment, we use the relational operators to compare the two strings.
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ how to compare strings in python
How to compare strings in Python - IONOS
November 21, 2023 - The easiest way to compare two Python strings is with Python operators. As with integers or floating-point numbers, comparison operators can be used to check that strings are equal. However, operators in this context donโ€™t work as they do with numbers, as there are several string properties ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-compare-strings-how-to-check-for-string-equality
Python Compare Strings โ€“ How to Check for String Equality
March 18, 2022 - When crafting the logic in your code, you may want to execute different commands depending on the similarities or differences between two or more strings. In this article, we'll see various operators that can help us check if strings are equal or no...
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-compare-strings
How To Compare Strings In Python?
January 23, 2025 - Python provides several operators for string comparison. The most used ones are ==, !=, <, >, <=, and >=. These operators compare strings lexicographically, meaning they compare based on the Unicode values of the characters. ... <=: Checks if the string on the left is lexicographically smaller than or equal to the string on the right.