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
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ thinkcspy โ€บ Strings โ€บ StringComparison.html
9.8. String Comparison โ€” How to Think like a Computer Scientist: Interactive Edition
Strings are compared character by character. ... Yes, upper case is less than lower case according to the ordinal values of the characters. ... Python is case sensitive meaning that upper case and lower case characters are different.
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".)

๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ string equals check in python: using 4 different methods
String Equals Check in Python: Using 4 Different Methods - AskPython
May 24, 2023 - The __eq__() method basically compares two objects and returns a boolean True or False. It returns True if strings are equivalent, otherwise, it returns False. ... str1 = "Python" str2 = "Python" str3 = "Java" if(str1.__eq__(str3)): print("str1 ...
๐ŸŒ
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. Case-insensitive comparisons are discussed later. ... To check for partial matches, use the in operator, which determines if one string contains another string.
๐ŸŒ
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.
๐ŸŒ
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 - While languages like C and C++ use ASCII codes for string comparison, Python uses Unicode values to compare characters. There are many occasions when we need to perform string comparisons.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/programmerhumor โ€บ no .equals()?
r/ProgrammerHumor on Reddit: No .equals()?
January 24, 2023 - If you want to know not simply if something tests as False or is equal to False, but whether it is โ€˜aโ€™ (the, because singleton) false Boolean value, you ask if it is False. Python is fun. More replies ... It might, it might not. If they come from the string pool, equals and == ate identical.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-comparison
Python Compare Strings - Methods & Best Practices | DigitalOcean
April 17, 2025 - By understanding the differences ... applications handle text correctly, regardless of the language or character set used. The equality operator == is used to compare two strings in Python....
๐ŸŒ
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. Result of > and < operator depends on Unicode values of letters at index where they are not the same.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-compare-strings
How To Compare Strings In Python?
January 23, 2025 - The most used ones are ==, !=, <, >, <=, and >=. These operators compare strings lexicographically, meaning they compare based on the Unicode values of the characters. ... <=: Checks if the string on the left is lexicographically smaller than or equal to the string on the right.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_operators_comparison.asp
Python Comparison Operators
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
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ python-string-comparison-methods
Python String Comparison: 8 Easy Ways You Must Know
Args: str_a: First string to compare str_b: Second string to compare Returns: bool: True if strings match exactly, False otherwise """ return str_a == str_b # Example usage first_string = "SecureDataComparison" second_string = "SecureDataComparison" if compare_strings_directly(first_string, second_string): print("Strings match!") else: print("Strings differ.") # Direct inequality check different_string = "securedatacomparison" if first_string != different_string: print("The strings are not equal.") else: print("The strings are equal.")
๐ŸŒ
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 ...
๐ŸŒ
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 ย  July 23, 2025
๐ŸŒ
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 ...
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_string_strcmp.php
C string strcmp() Function
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT SWIFT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST
๐ŸŒ
Quora
quora.com โ€บ How-do-I-compare-two-strings-in-Python
How to compare two strings in Python - Quora
Answer (1 of 3): The following are the ways to compare two string in Python: 1. By using [code ]== (equal to)[/code] operator 2. By using [code ]!= (not equal to)[/code] operator 3. By using [code ]sorted()[/code] method 4. By using [code ]is[/code] ...