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
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-comparison
Python Compare Strings - Methods & Best Practices | DigitalOcean
April 17, 2025 - Learn how to compare strings in Python using ==, !=, startswith(), endswith(), and more. Find the best approach for your use case with examples.
🌐
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
People also ask

What is string comparison in Python?
String comparison in Python refers to checking if two strings are equal, similar, or meet certain conditions, using operators or functions like ==, !=, or regular expressions.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string comparison in python
String Comparison in Python | Operators & Examples Guide
Is there any built-in Python function to compare two strings for similarity?
While Python doesn't have a specific function for string similarity, you can use difflib or regular expressions to compare two strings for similarity based on patterns or partial matches.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string comparison in python
String Comparison in Python | Operators & Examples Guide
How do I compare two strings in Python for equality?
You can compare two strings for equality using the == operator, which checks if both strings have identical content, as shown in a string comparison in Python example.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string comparison in python
String Comparison in Python | Operators & Examples Guide
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".)

🌐
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 string)). I want to do a simple ‘if’ statement, but since I won’t ...
🌐
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 that can be compared.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string comparison in python
String Comparison in Python | Operators & Examples Guide
November 13, 2024 - The == operator checks if two strings are exactly equal in content. In the example above, string1 and string2 are both "hello", so string1 == string2 returns True. This is a string comparison in Python example.
Find elsewhere
🌐
Index.dev
index.dev › blog › python-string-comparison-methods
Python String Comparison: 8 Easy Ways You Must Know
This means characters are compared based on their numerical Unicode values. When we compare strings, Python looks at each character pair from left to right until it hits a difference or runs out of characters in one string.
🌐
FavTutor
favtutor.com › blogs › compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - String comparison is a fundamental operation in Python programming. It allows us to compare two strings and determine their relative order, equality, and other characteristics. Whether you're sorting a list of names or searching for specific ...
🌐
Educative
educative.io › answers › how-to-compare-two-strings-in-python
How to compare two strings in Python
To compare two strings, we mean that we want to identify whether the two strings are equivalent to each other or not, or perhaps which string should be greater or smaller than the other.
Top answer
1 of 15
1695

is is identity testing, and == is equality testing. What happens in your code would be emulated in the interpreter like this:

>>> a = 'pub'
>>> b = ''.join(['p', 'u', 'b'])
>>> a == b
True
>>> a is b
False

So, no wonder they're not the same, right?

In other words: a is b is the equivalent of id(a) == id(b)

2 of 15
672

Other answers here are correct: is is used for identity comparison, while == is used for equality comparison. Since what you care about is equality (the two strings should contain the same characters), in this case the is operator is simply wrong and you should be using == instead.

The reason is works interactively is that (most) string literals are interned by default. From Wikipedia:

Interned strings speed up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on hash tables with string keys. Without interning, checking that two different strings are equal involves examining every character of both strings. This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fills up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.

So, when you have two string literals (words that are literally typed into your program source code, surrounded by quotation marks) in your program that have the same value, the Python compiler will automatically intern the strings, making them both stored at the same memory location. (Note that this doesn't always happen, and the rules for when this happens are quite convoluted, so please don't rely on this behavior in production code!)

Since in your interactive session both strings are actually stored in the same memory location, they have the same identity, so the is operator works as expected. But if you construct a string by some other method (even if that string contains exactly the same characters), then the string may be equal, but it is not the same string -- that is, it has a different identity, because it is stored in a different place in memory.

🌐
Note.nkmk.me
note.nkmk.me › home › python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - This article explains string comparisons in Python, covering topics such as exact matches, partial matches, forward/backward matches, and more. Exact match (equality comparison): ==, != Partial match: ...
🌐
Python Guides
pythonguides.com › python-compare-strings
How To Compare Strings In Python?
January 23, 2025 - To compare strings in Python, you can use basic comparison operators like ==, !=, <, >, <=, and >=. These operators compare strings lexicographically, based on the Unicode values of the characters.
🌐
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.
🌐
CodeGym
codegym.cc › java blog › learning python › how to compare strings in python
How To Compare Strings in Python
November 11, 2024 - In Python, comparing strings involves evaluating whether two strings are equal, whether one string is “greater than” another (alphabetically), or if they match according to specific criteria (like case-insensitivity).
🌐
Flexiple
flexiple.com › python › python-string-comparison
Various methods for Python String Comparison - Flexiple Tutorials - Flexiple
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.
🌐
Career Karma
careerkarma.com › blog › python › python compare strings: a step-by-step guide
Python Compare Strings: A Step-By-Step Guide | Career Karma
December 1, 2023 - Python comparison operators can be used to compare strings in Python. These operators are: equal to (==), not equal to (!=), greater than (>), less than (<), less than or equal to (<=), and greater than or equal to (>=).
🌐
Hostman
hostman.com › tutorials › how to compare python strings
Compare strings in Python: A Step-by-Step Guide | Hostman
December 24, 2025 - Enter first string: New York Enter second string: Washington The sequences New York and Washington are different! The next two methods are startswith() and endswith(). The former is useful when you want to compare the elements of a Python string located at the beginning of the string against a given character pattern.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
ReqBin
reqbin.com › code › python › l1pjhl0c › python-compare-strings-example
How do I compare strings in Python?
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.