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

🌐
Replit
replit.com › home › discover › how to compare strings in python
How to compare strings in Python | Replit
April 14, 2026 - It’s important to remember that these comparisons are case-sensitive, meaning "Hello" would not be equal to "hello". Since basic equality checks are case-sensitive, you'll often need more flexible techniques for tasks like sorting data or ignoring capitalization in user input. ... string1 = "Python" string2 = "python" print(string1 == string2) # Case-sensitive comparison print(string1.lower() == string2.lower()) # Case-insensitive comparison
🌐
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.
🌐
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)
🌐
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' == ...
Find elsewhere
🌐
Index.dev
index.dev › blog › python-string-comparison-methods
Python String Comparison: 8 Easy Ways You Must Know
May 15, 2025 - 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 - 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.
🌐
FavTutor
favtutor.com › blogs › compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - Similarly for situations like checking for a valid email address, checking the name that exists in a record, etc; we require string comparisons. In python language, we can compare two strings such as identifying whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other.
🌐
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 ...
🌐
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 - Regular expressions provide powerful tools for advanced string comparison. They allow for pattern matching, substitution, and more complex string operations. Utilize the ‘re’ module in Python to leverage regular expressions.
🌐
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.
🌐
Medium
medium.com › @blogshub4 › how-to-compare-strings-in-python-8692d9e68689
How to Compare Strings in Python. String comparison is a common operation… | by Blogshub | Medium
December 28, 2024 - This guide explores all the methods you can use to compare strings, including case-insensitive comparisons and object-based evaluations. ... Strings in Python are sequences of characters stored as arrays of 16-bit Unicode (or 8-bit ASCII in Python 2). Strings are immutable, meaning once created, they cannot be modified.
🌐
Flexiple
flexiple.com › python › python-string-comparison
Various methods for Python String Comparison - Flexiple Tutorials - Flexiple
March 10, 2022 - Python string comparison methods are used to compare strings. However, Python also comes with a few handy inbuilt operators to facilitate this.
🌐
Squash
squash.io › string-comparison-in-python-best-practices-and-techniques
String Comparison in Python: Best Practices and Techniques
May 21, 2024 - Python's built-in re module provides powerful regular expression functionality to compare and manipulate strings based on complex patterns. Regular expressions can be used for advanced string comparisons and pattern matching.
🌐
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}")
🌐
ReqBin
reqbin.com › code › python › l1pjhl0c › python-compare-strings-example
How do I compare strings in Python?
July 27, 2023 - If the first line is a prefix of the second, then it is less than the second. If two lines start the same, but then differ, then less is the line with the first differing character less.
🌐
Hostman
hostman.com › tutorials › how-to-compare-python-strings
Compare strings in Python: A Step-by-Step Guide | Hostman
In this chapter, we will compare sequences of characters entered from the keyboard. For this purpose, we will use the comparison operators (==, !=, <, >, <=, >=), explained in the previous chapter. So, in order to compare two strings in Python entered from the keyboard, let's write the following ...