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".)

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-comparison
Python Compare Strings - Methods & Best Practices | DigitalOcean
April 17, 2025 - This discrepancy occurs because ... Unicode code point value of the characters. In Python, there are three primary methods for comparing strings: ==, is, and cmp()....
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Comparing Strings - Python Help - Discussions on Python.org
July 11, 2023 - Hello, I am attempting to teach myself Python using MITโ€™s OCW. I am trying to program a Hangman game using functions and I need to compare a string (letters_guessed) to the word the computer chose (secret_word (also a stโ€ฆ
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_string_strcmp.php
C string strcmp() Function
For this comparison characters at the same position from both strings are compared one by one, starting from the left until one of them does not match or the end of a string has been reached.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - Similar to numbers, the == operator checks if two strings are equal. If they are equal, True is returned; otherwise, False is returned. print('abc' == 'abc') # True print('abc' == 'xyz') # False ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-comparison-in-python
String Comparison in Python - GeeksforGeeks
... s1 = "apple" s2 = "banana" ... lexicographically, therefore it is True print(s1 < s2) ... The == operator is a simple way to check if two strings are identical....
Published ย  July 23, 2025
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ thinkcspy โ€บ Strings โ€บ StringComparison.html
9.8. String Comparison โ€” How to Think like a Computer Scientist: Interactive Edition
When you compare characters or strings to one another, Python converts the characters into their equivalent ordinal values and compares the integers from left to right.
Find elsewhere
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ python-string-comparison-methods
Python String Comparison: 8 Easy Ways You Must Know
Direct equality (==) and inequality (!=) operators perform character-by-character comparison, which is O(n) complexity where n is the length of the strings. Python optimizes this operation by first checking string lengths and identical string objects before performing the full comparison.
๐ŸŒ
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 - Although the way you want to compare ... While languages like C and C++ use ASCII codes for string comparison, Python uses Unicode values to compare characters....
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ comparing-strings-using-python
Comparing Strings using Python
June 21, 2023 - This order depends on the character table that is in use on your machine while executing the Python code. Keep in mind the order is case-sensitive. As an example for the Latin alphabet, "Bus" comes before "bus". Listing 2 shows how these comparison operators work in practice. ... # Define the strings listOfPlaces = ["Berlin", "Paris", "Lausanne"] currentCity = "Lausanne" for place in listOfPlaces: if place < currentCity: print (f"{place} comes before {currentCity}") elif place > currentCity: print (f"{place} comes after {currentCity}") else: print (f"{place} is equal to {currentCity}")
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ articles โ€บ compare-strings-in-python
Compare strings in Python
This fact can be verified by checking their id() value. ... Hence, comparison operator == for checking equality returns True if two string operands have same id() value, and False otherwise.
๐ŸŒ
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 ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ compare strings using either โ€˜==โ€™ or โ€˜isโ€™ sometimes produces a different result.
Compare strings using either '==' or 'is' sometimes produces a different result. - Spark By {Examples}
May 31, 2024 - In the above example, we have three string objects: a, b, and c. When we concatenate a and b with a space in between, we create a new string object c. Surprisingly, using the โ€˜isโ€™ operator to compare c with the concatenated string 'hello world' returns False. This unexpected behavior occurs because the string 'hello world' is not interned, while c is a new string created through concatenation. Python doesnโ€™t intern the result of string concatenation in the same way it interns string literals.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-compare-strings
How To Compare Strings In Python?
January 23, 2025 - For instance, city1 == city2 checks if the strings city1 and city2 are equal, while city1 < city2 checks if city1 is lexicographically smaller than city2. ... Python provides several operators for string comparison.
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ how to compare python strings
Compare strings in Python: A Step-by-Step Guide | Hostman
December 24, 2025 - To compare two strings in Python, you need to learn how the comparison operators work. In the table below, we will break down the existing comparison operators and give examples to help you understand how they work. In the examples, we will use unequal sequences of characters "dog" and "cat."
Price ย  $
Call ย  +1 844 286 2130
Address ย  1999 Harrison St 1800 9079, 94612, Oakland
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-compare-two-python-strings-for-equality-in-a-case-insensitive-manner-395043
How to compare two Python strings for equality in a case-insensitive manner? | LabEx
Let's modify our script to add some examples that demonstrate when case-insensitive comparison is helpful: ## Add these examples to string_comparison.py ## Example: User searching for content user_search = "Python" article_title = "Getting Started with python Programming" ## Case-sensitive comparison (might miss relevant content) found_sensitive = user_search in article_title print(f"Case-sensitive search found match: {found_sensitive}") ## What if we want to find matches regardless of case?
๐ŸŒ
Reddit
reddit.com โ€บ r/cs50 โ€บ how to compare string properly
r/cs50 on Reddit: How to compare string properly
May 6, 2021 -

Hi everyone,
I'm stuck on the pset 2 substitution. And I'm not gonna lie i'm struggling big time. So I have divided the problem in small parts.

First, I'm trying to create a function to verify the validity of the key. For doing so i want to compare the user input key to an arbitrary constant named KEY (bb steps) and printing a 1 if the input correspond to the constant or a 0 if the input doesn't correspond.

i'm not using bool for simplicity

Here is my code :

The only result that i have is 0, even when I type the correct key which is hello here. But why? my syntax seems good.
Thank for your responses
P.S. i will gladly use a more efficient way to share my code if you have one