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

String comparison in Python: is vs. == - Stack Overflow
In my case it's not worth the risk ... the strings in the future. 2012-10-30T10:56:53.233Z+00:00 ... For anyone looking at this years later, this still holds true for Python 3. 2013-11-15T22:07:22.9Z+00:00 ... Save this answer. ... Show activity on this post. I would like to show a little example on how is ... More on stackoverflow.com
🌐 stackoverflow.com
How does python compare two strings??
Sorting strings happens by the unicode value of the characters. >>> ord('a') 97 >>> ord('b') 98 Since 97 is less than 98, 'a' < 'b' returns True. More on reddit.com
🌐 r/learnpython
6
7
June 15, 2020
How to search strings in Python using a wildcard that represents only a single word -- and not multiple words as well?
You can use regular expressions for that. https://www.geeksforgeeks.org/pattern-matching-python-regex/ More on reddit.com
🌐 r/learnpython
12
33
January 26, 2023
Performance comparison of String formatting Methods using timeit library
Good to know. Serious question though: Has anybody had a use case where string formating was the performance bottleneck? More on reddit.com
🌐 r/Python
80
378
September 6, 2021
🌐
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
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".)

🌐
Cherry Servers
cherryservers.com › home › blog › cloud computing › how to do string comparison in python [with examples]
How to do String Comparison in Python [With Examples]
November 7, 2025 - Therefore, the second comparison returns False. Similarly, when comparing "hello123" and "world456", the function returns True as they have the same number of digits. You can also compare strings with the startswith() and endswith() methods. Here are some examples of using Python str methods.
🌐
Note.nkmk.me
note.nkmk.me › home › python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - If they are equal, True is returned; otherwise, False is returned. print('abc' == 'abc') # True print('abc' == 'xyz') # False ... This operation is case-sensitive, as are other comparison operators and methods.
🌐
FavTutor
favtutor.com › blogs › compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - For example, the equality operator (==) is commonly used for general string equality checks, while the relational operators (<, >, <=, >=) are used for lexicographical comparisons.
Find elsewhere
🌐
ReqBin
reqbin.com › code › python › l1pjhl0c › python-compare-strings-example
How do I compare strings in Python?
July 27, 2023 - You can also compare strings using ... also use the '<' and '>' operators to compare strings. In this Python Compare Strings example, we are comparing strings using the '==' operator. Other string comparison methods are provided below, with detailed examples and explana...
🌐
Squash
squash.io › string-comparison-in-python-best-practices-and-techniques
String Comparison in Python: Best Practices and Techniques
May 21, 2024 - In this example, we have two strings string1 and string2 that contain the word "Café", but string2 uses a different representation with a combining acute accent character (\u0301).
🌐
IONOS
ionos.com › digital guide › websites › web development › how to compare strings in python
How to compare strings in Python - IONOS
November 21, 2023 - For example, Python evaluates the ex­pres­sion “a < b” as “true”, but the ex­pres­sion “a < B” as “false”. That’s because in both ASCII and Unicode standards, the uppercase letters are always encoded with lower values than the lowercase letters. ... In addition to comparing strings, for­mat­ting texts is an important part of pro­gram­ming.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-equals
How to Check if Two Strings Are Equal in Python (With Examples) | DigitalOcean
Learn how to compare strings in Python using ==, !=, and other methods. This guide covers edge cases, best practices, and real-world examples.
🌐
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)
🌐
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}")
🌐
CodeGym
codegym.cc › java blog › learning python › how to compare strings in python
How To Compare Strings in Python
November 11, 2024 - One thing that might seem confusing is the difference between is and ==. In Python, == checks if two strings have the same value, while is checks if they refer to the exact same object in memory. string1 = "hello" string2 = "hello" string3 = string1 ...
🌐
freeCodeCamp
freecodecamp.org › news › python-compare-strings-how-to-check-for-string-equality
Python Compare Strings – How to Check for String Equality
March 18, 2022 - So make sure you use == for comparison. The != operator checks if two strings are not equal. string1 = "Hello" string2 = "Hello" if string1 != string2: print("Both strings are not equal") # return if true else: print("Both strings are equal") # return if false # Both strings are equal · We're using the same example but with a different operator.
🌐
Flexiple
flexiple.com › python › python-string-comparison
Various methods for Python String Comparison - Flexiple Tutorials - Flexiple
March 10, 2022 - Python saves memory by re-using object IDs containing the same value; eg: here. This also makes python string comparison faster and easier.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python › python compare strings (==, ignore case, substring, examples)
Python Compare Strings (==, Ignore Case, Substring, Examples) | GoLinuxCloud
April 12, 2026 - Case-insensitive comparison is required when user input should not depend on capitalization. ... Use the in operator to check substring presence. For dedicated membership tests and case-insensitive checks, see check if a string contains a substring. ... For more substring-related operations, check Python substring.
🌐
Unstop
unstop.com › home › blog › 12 ways to compare strings in python with examples
12 Ways To Compare Strings In Python With Examples
March 18, 2024 - This type of string comparison ... case-sensitive comparisons in Python: The equality operator (==), for example- string1 == string2...
🌐
AskPython
askpython.com › home › string comparison in python
String Comparison in Python - AskPython
August 6, 2022 - Python is Operator returns True if two variables refer to the same object instance. str1 = "DEED" str2 = "DEED" str3 = ''.join(['D', 'E', 'E', 'D']) print(str1 is str2) print("Comparision result = ", str1 is str3) ...
🌐
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.