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
680

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

🌐
GeeksforGeeks
geeksforgeeks.org › python › string-comparison-in-python
String Comparison in Python - GeeksforGeeks
Python supports several operators for string comparison, including ==, !=, <, <=, >, and >=. These operators allow for both equality and lexicographical (alphabetical order) comparisons, which is useful when sorting or arranging strings.
Published: March 18, 2026
Discussions

Comparing Strings
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 string)). I want to do a simple ‘if’ statement, but since I won’t ... More on discuss.python.org
🌐 discuss.python.org
19
0
July 11, 2023
Does the == operator compare strings by value or address?
The equality operator compares values. if you want to compare memory location or identity, use the is operator. More on reddit.com
🌐 r/learnpython
13
3
January 14, 2020
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-comparison
Python Compare Strings: Methods, Operators & Best Practices | DigitalOcean
Learn how to compare strings in Python using ==, is, and ordering operators, plus casefold() and difflib for tricky cases.
🌐
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…
🌐
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, com­par­i­son 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 prop­er­ties that can be compared.
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - Search for a string in Python (Check if a substring is included/Get a substring position) Similar to numbers, the == operator checks if two strings are equal. If they are equal, True is returned; otherwise, False is returned. print('abc' == ...
🌐
Medium
medium.com › data-science › side-by-side-comparison-of-strings-in-python-b9491ac858
Side-by-side comparison of strings in Python | by Leo van der Meulen | TDS Archive | Medium
July 17, 2021 - It will not be used to compare books of texts, but only a few lines at a time. In order to match sequences we first have to create an instance of the SequenceMatcher class. The constructor takes the two sequences to match as parameters a and b. The parameters for junk detection are not used. When passing strings as arguments, a string is seen as a sequence of characters.
🌐
YouTube
youtube.com › watch
Python Tutorial: Comparing Strings - YouTube
In this video, we discuss how Python compares strings. What's it mean for a string to be less than another string? What's an ASCII code? Are two strings t...
Published: June 3, 2020
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
Since strings are arrays, we can loop through the characters in a string, with a for loop. ... Learn more about For Loops in our Python For Loops chapter.
🌐
ReqBin
reqbin.com › code › python › l1pjhl0c › python-compare-strings-example
How do I compare strings in Python?
July 27, 2023 - The easiest way to compare Python strings is to use the '==' and '!=' boolean operators. You can also compare strings using the 'is' and 'is not' operators, which are primarily used to compare objects in Python.
🌐
Cisco
ipcisco.com › home › python string comparison
Python String Comparison | How to Compare Python Strings? ⋆ IpCisco
December 27, 2021 - We can compare python strings with different signs like equality (==) and comparison (<, >, <=, >= , !=) operators.
🌐
Replit
replit.com › home › discover › how to compare strings in python
How to compare strings in Python | Replit
April 14, 2026 - You can also use slicing, like text[:6], to extract a portion of the string and compare it with another using the == operator. While the common methods are great for exact matches, advanced scenarios often demand more flexible tools for pattern matching, measuring similarity, and handling complex characters. ... import re text = "Python 3.9.5" version_pattern = r"Python \d\.\d\.\d" version_match = re.match(version_pattern, text) print(bool(version_match)) print(re.search(r"\d+\.\d+", text).group()) # Extract version number
🌐
GeeksforGeeks
geeksforgeeks.org › python › case-insensitive-string-comparison-in-python
Case-insensitive string comparison in Python - GeeksforGeeks
July 15, 2025 - Explanation: This code converts all strings in the list to lowercase for case-insensitive comparison, stores them in a new list b and checks if all elements are identical by converting the list to a set. If the set has only one unique element, it prints "equal" otherwise, "unequal". casefold() method in Python performs a case-insensitive comparison that accounts for locale-specific character differences.
🌐
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 strings depends on the use case at hand, the most common method is to compare the characters of each string from left to right. While languages like C and C++ use ASCII codes for string comparison, Python uses Unicode values to compare characters.
🌐
Flexiple
flexiple.com › python › python-string-comparison
Various methods for Python String Comparison - Flexiple Tutorials - Flexiple
March 10, 2022 - So these relation operators compare strings based on their Unicode values. The "==" is a python string comparison method that checks if both the values of the operands are equal.
🌐
Medium
medium.com › @sami.hamdiapps › elevate-your-python-skills-string-comparison-python-made-easy-bd0b3b08b55b
Elevate Your Python Skills: String Comparison Python Made Easy | by Sami Hamdi | Medium
September 4, 2023 - Python provides four fundamental operators to compare strings: == (equal),!= (not equal), (less than), and > (greater than). These operators determine the relationship between two strings based on their alphabetic order — in other words, their ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string comparison in python
String Comparison in Python | Operators & Examples Guide
June 2, 2025 - Python, the most common relational operators are == and !=. These · operators allow you to check if two strings are equal or not equal to each other. ... # String Comparison using == and != operators string1 = "hello" string2 = "hello" string3 ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › compare strings in python: essential operators & best practices
Compare Strings In Python: Essential Operators & Best Practices - Analytics Vidhya
January 9, 2024 - Compare strings in Python with '==' operator & other methods. Learn how to sort, search & filter data for fundamental operations in Python.