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
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - In python language, we can compare ... of the string comparison operators used for this purpose below: ==: This operator checks whether two strings are equal....
๐ŸŒ
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 - We got True because both strings are equal. The > operator checks if one string is greater than another string.
Discussions

String comparison in Python: is vs. == - Stack Overflow
The spec says "For all built-in ... not "For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x==y, then x is y is also True." For some reason, you're pretending it says the latter. It doesn't. You see that equality matches, but is does... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
How to check the specific element in string are equal in python? - Stack Overflow
There is a two input first input represent the index of the second input.I need to find the 'X' position of second input are same or not? input: 123X56XX Ahjutruu Output: True Example: X in inp... More on stackoverflow.com
๐ŸŒ stackoverflow.com
In case you didn't know: Python 3.8 f-strings support = for self-documenting expressions and debugging
f-strings is one of the best things for python 3 More on reddit.com
๐ŸŒ r/Python
108
1753
August 26, 2020
๐ŸŒ
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 - The == equality operator returns True if two values match; otherwise, the operator returns False. The != operator returns True if two values do not match, and False if two values match.
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".)

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-comparison
Python Compare Strings - Methods & Best Practices | DigitalOcean
April 17, 2025 - You can compare strings in Python using the equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-equals
How to Check if Two Strings Are Equal in Python (With Examples) | DigitalOcean
September 12, 2025 - This operator checks if the value of the variable is equal to the string. ... Yes, you use the == operator for strings in Python.
Find elsewhere
๐ŸŒ
Pythonspot
pythonspot.com โ€บ python-strings
python if string equals - Python Tutorial
April 4, 2017 - This is the location of python ... index is always [0]. The last index is the length of the string minus one. ... The equality operator (==) tests if two variables have an equal value....
๐ŸŒ
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.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ thinkcspy โ€บ Strings โ€บ StringComparison.html
9.8. String Comparison โ€” How to Think like a Computer Scientist: Interactive Edition
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.
๐ŸŒ
YouTube
youtube.com โ€บ watch
52. Comparing Strings - Learn Python - YouTube
Download the Wing 101 Integrated Development Environment(IDE) - http://wingware.com/downloads/wing-101โค๏ธ Other playlistsMore from this playlist, Learn Python...
Published ย  March 6, 2018
๐ŸŒ
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 - Python provides four fundamental operators to compare strings: == (equal),!= (not equal), (less than), and > (greater than). These operators determine the relationship between two strings based on their alphabetic order โ€” in other words, their ...
๐ŸŒ
Quora
quora.com โ€บ Why-are-strings-not-equal-to-each-other-even-after-comparing-using-the-operator-in-Python
Why are strings not equal to each other even after comparing using the == operator in Python? - Quora
Answer: I understand that this should give you True if both strings have the same value. Take a careful look at the string. Watch out especially whether the two strings have the same case. None of the following three strings is equal to the others, simply because the letters donโ€™t have the ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-comparison-in-python
String Comparison in Python - GeeksforGeeks
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
๐ŸŒ
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 ... The following are the ways to perform case-sensitive comparisons in Python: The equality operator (==), for example- string1 == string2...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings.asp
Python Strings
Strings in python are surrounded by either single quotation marks, or double quotation marks. ... print("It's alright") print("He is called 'Johnny'") print('He is called "Johnny"') Try it Yourself ยป ยท Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
3 days ago - Some operations are supported by several object types; in particular, practically all objects can be compared for equality, tested for truth value, and converted to a string (with the repr() function or the slightly different str() function).
๐ŸŒ
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: ...