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
🌐
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
🌐
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.
Discussions

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
[Python] TIL Python's "or" is a null coalescing operator
This is not "coalescing" anything, the operator works on "truthiness" not actual bools. It looks at the left and returns it if it's truthy, otherwise it returns the right. >>> repr(False or None) 'None' >>> repr([] or {}) '{}' >>> repr(True or 1) 'True' More on reddit.com
🌐 r/ProgrammerTIL
22
33
July 19, 2016
What's the difference between != and is not?
I'm pulling your leg on this with this comment since u/kberson already answered your real question. But the the answer to your stated question is: there is no effective difference between: if x != 5; print(x) and if x is not 5; print(x) because they both result in a syntax error. Use : not ; More on reddit.com
🌐 r/learnpython
119
332
April 7, 2020
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
What does the is operator do in string comparison?
The is operator checks if two variables reference the same object in memory, which can be useful for identity comparison, but not for checking string values.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string comparison in python
String Comparison in Python | Operators & Examples Guide
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".)

🌐
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 below table provides an overview of com­par­i­son operators and their functions. Here, “lex­i­co­graph­ic order” is the technical term for “al­pha­bet­i­cal order” and means the same thing. Words with a low lex­i­co­graph­ic order come earlier in the alphabet than words with a high lex­i­co­graph­ic order. So, the string “abcd” has a lower lex­i­co­graph­ic order than the string “xyz”.
🌐
Note.nkmk.me
note.nkmk.me › home › python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - To check for partial matches, use the in operator, which determines if one string contains another string. x in y returns True if x is contained in y (i.e., x is a substring of y), and False if it is not.
🌐
Runestone Academy
runestone.academy › ns › books › published › thinkcspy › Strings › StringComparison.html
9.8. String Comparison — How to Think like a Computer Scientist: Interactive Edition
To see if two strings are equal you simply write a boolean expression using the equality operator. Other comparison operations are useful for putting words in lexicographical order.
Find elsewhere
🌐
Medium
yashvaantlakham73.medium.com › string-comparison-in-python-834418a69686
String Comparison in Python. Unleash the power of precision | by A.I Hub | Medium
October 14, 2024 - As usual, they return a Boolean value True or False. Two strings are considered equal if their content is exactly the same. ... The comparisons performed by the comparison operators are case-sensitive. For example, 'Python' and 'python' will not be considered equal.
🌐
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.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string comparison in python
String Comparison in Python | Operators & Examples Guide
June 2, 2025 - 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.
🌐
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 - Learn More: String Data Structure in Python: A Complete Case Study · Let’s explore some common string comparison scenarios and examples: To check if two strings are equal, use the ‘==’ operator or the ‘cmp()’ function.
🌐
Squash
squash.io › string-comparison-in-python-best-practices-and-techniques
String Comparison in Python: Best Practices and Techniques
May 21, 2024 - There are many python comparison operators, such as <, <=, >, >=, ==, and !=. These operators allow you to check if one string is greater than, less than, equal to, or not equal to another string.
🌐
FavTutor
favtutor.com › blogs › compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - String comparison is the process of comparing two strings to determine their relative order, equality, or other characteristics. Python provides various methods and operators that enable us to perform string comparison efficiently.
🌐
Stack Abuse
stackabuse.com › comparing-strings-using-python
Comparing Strings using Python
June 21, 2023 - $ python3 comparing-strings.py comparing Berlin with Lausanne: False comparing Paris with Lausanne: False comparing Lausanne with Lausanne: True · Python has the two comparison operators == and is.
🌐
Python Guides
pythonguides.com › python-compare-strings
How to Compare Strings in Python
January 19, 2026 - I’ve seen many developers mistakenly use is to compare strings. The is operator checks for identity (if they are the same object in memory), not equality (if the values are the same). In my experience, you should almost always stick to ==. # This might work due to Python's interning, but it's risky a = "Austin" b = "Austin" print(a is b) # True (usually, but don't rely on it) # Use == instead print(a == b) # True (always correct for value 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 - In this example, the lower() method converts the complete strings to their lowercase form before the comparison. That's why you get True with the == operator and False with the < operator. In the above examples, we compared strings based on their Unicode values. However, if you want to compare strings based on a different criterion, you will need to write a custom Python user-defined function.
🌐
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.
🌐
Codingem
codingem.com › home › python string comparison: a step-by-step guide (with examples)
Python String Comparison: A Step-by-Step Guide (with Examples)
July 10, 2025 - To compare strings alphabetically, you can use the operators <, >, <=, >=. For instance, let’s compare the names “Alice” and “Bob”. This comparison corresponds to checking if Alice is before Bob in alphabetical order.
🌐
Board Infinity
boardinfinity.com › blog › string-comparision-in-python
String Comparison in Python | Board Infinity
August 18, 2025 - Depending on the string comparison, the print() function will either show True or False. ... The |(== ) operator in Python is used to compare the values of the two operands and determine whether they are equal.
🌐
Scientech Easy
scientecheasy.com › home › blog › string comparison in python (with examples)
String Comparison in Python (with Examples) - Scientech Easy
January 26, 2026 - We can compare strings using various comparison or relational operators. For example, we can use the double equal sign (==) operator for string equality. For string inequality, use (!=) sign. In the above example program, we have created two strings “january” and “jane” and stored in ...