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
๐ŸŒ
W3Schools
w3schools.in โ€บ python โ€บ examples โ€บ compare-strings
Comparing Strings in Python
This tutorial demonstrates Python programs for comparing strings. The purpose of this tutorial is to explain to beginners how to use the different comparison operators with conditional statements. String comparison in Python is the process of comparing two strings to check if they are equal or not.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-comparison-in-python
String Comparison in Python - GeeksforGeeks
s1 = "Python" s2 = "Python" # Since ... is True print(s1 == s2) ... Explanation: In this example, since s1 and s2 have the same characters in the same order, so == returns True. The != operator helps to verify if two strings are different. If the strings are different then it will return True, otherwise returns False. ... Explanation: Here, != checks that s1 and s2 are not the same, so it returns True. Lexicographical comparison checks if one string appears before or after another in alphabetical order...
Published ย  July 23, 2025
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings.asp
Python Strings
Learn more about If statements in our Python If...Else chapter. To check if a certain phrase or character is NOT present in a string, we can use the keyword not in. Check if "expensive" is NOT present in the following text: txt = "The best things in life are free!" print("expensive" not in txt) Try it Yourself ยป ... txt = "The best things in life are free!" if "expensive" not in txt: print("No, 'expensive' is NOT present.") Try it Yourself ยป ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_string.asp
Python String Methods
Python HOME Python Intro Python Get Started Python Syntax ... Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types ... Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans ... Python Operators Arithmetic Operators Assignment Operators Comparison ...
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".)

๐ŸŒ
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: ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_comparison_operators.asp
Python Comparison Operators
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Python Operators Tutorial Operators Arithmetic Operators Assignment Operators Logical Operators Identity Operators Membership Operators Bitwise Operators ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_check_string.asp
Python Check In String
Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative Indexing on a String String Length Format String Escape Characters ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
๐ŸŒ
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 ...
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ string comparison in python
String Comparison in Python | Operators & Examples Guide
January 4, 2026 - 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 ...
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ string comparison in python
String Comparison in Python - AskPython
August 6, 2022 - If we wish to compare two strings and check for their equality even if the order of characters/words is different, then we first need to use sorted() method and then compare two strings.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ thinkcspy โ€บ Strings โ€บ StringComparison.html
9.8. String Comparison โ€” How to Think like a Computer Scientist: Interactive Edition
The way you can find out the so-called ordinal value for a given character is to use a character function called ord. When you compare characters or strings to one another, Python converts the characters into their equivalent ordinal values and compares the integers from left to right.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-compare-strings
How To Compare Strings In Python?
January 23, 2025 - Now, let me show you some examples of how to compare strings in Python using the above operators. # Example strings city1 = "New York" city2 = "Los Angeles" city3 = "Chicago" # Equality print(city1 == city2) # Output: False # Inequality print(city1 != city3) # Output: True # Lexicographical comparison print(city1 < city2) # Output: True print(city2 > city3) # Output: True print(city3 <= city1) # Output: True print(city3 >= city2) # Output: False
๐ŸŒ
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 - Comparing strings in Python is a frequent task, and understanding its available methods and techniques is vital for effective programming. In this guide, we have covered both basic string comparison Python using operators as well as advanced comparison techniques like regular expressions.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ articles โ€บ compare-strings-in-python
Compare strings in Python
Comparison operators ==, !=, <, > <= and >= perform comparison of strings according to lexicographic order of letter. Unicode values of letters in each string are compared one by one.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ string comparison in python
How to do String Comparison in Python?
February 15, 2024 - Learn how to perform String Comparison in Python using various operators and methods. Scaler Topics explain each comparison with examples.
๐ŸŒ
Miguendes
miguendes.me โ€บ python-compare-strings
How to Compare Two Strings in Python (in 8 Easy Ways)
November 28, 2021 - Learn to compare if two strings are equal, or similar. Take string difference. Make comparisons ignoring case, and check if they are almost equal.