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 ... Let's start with a simple example to illustrate these operators. ... The == operator is a simple way to check if ......
Published   March 18, 2026
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".)

Discussions

Comparing strings in if statements
if pizza == str(yes): This will produce an error message saying that yes has not been declared. Look up the difference between variables and strings. More on reddit.com
🌐 r/learnpython
11
1
June 20, 2024
Comparing Strings
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 ... More on discuss.python.org
🌐 discuss.python.org
19
0
July 11, 2023
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
Python: How can i compare a User-Input-String in an if-Statement? - Stack Overflow
i know this question may sound stupid, but i´m having problems to check/compare Strings in an if-Statement. I´m new to Python and we need to make a little project for our school work in python. I d... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-comparison
Python Compare Strings: Methods, Operators & Best Practices | DigitalOcean
June 15, 2026 - You can use the == operator to compare multiple strings at once. This can be done by chaining multiple == operators together. For example: str1 = "Hello, World!" str2 = "Hello, World!" str3 = "Hello, World!" print(str1 == str2 == str3) This prints True only if every string in the chain is equal ...
🌐
Reddit
reddit.com › r/learnpython › comparing strings in if statements
r/learnpython on Reddit: Comparing strings in if statements
June 20, 2024 -

I'm writing this through my phone, forgive me if the proper indentation is incorrect, as it displays differently in my screen, and auto correct might mess something up. I'm getting the following error: Name error = name 'yes' is not defined. I dug around, and this usually happens when the variable is not defined, or if the value is pulled before the variable being defined. I don't think the issue is with my logic, but moreso with the syntax. I even tried specifying that it was a string value, as that was a problem once, but it doesn't seem to work either. If I could get any help, I'd appreciate it.

Here's the code that's giving the error:

pizza = str(input("Do you like pizza? "))

If pizza == str(yes): print("User Likes Pizza") elif pizza == no: print("user does not like pizza") else print("Please Give Valid response")

🌐
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 - Python Comparison operators can be used to compare strings and check for their equality in a case-sensitive manner i.e. uppercase letters and lowercase letters would be treated differently. Python ‘==’ operator compares the two strings in a character-by-character manner and returns True if both ...
🌐
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 st…
🌐
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.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-equals
How to Check if Two Strings Are Equal in Python (With Examples) | DigitalOcean
September 12, 2025 - To fix this, use the == operator for string comparison. ... s1 = 'Hello' s2 = 'Hello' print(s1 is s2) # This will return True if s1 and s2 refer to the same object, not if they have the same value. ... When comparing a string to None or non-string types, Python will raise a TypeError.
🌐
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 - Additionally, avoid unnecessary string concatenation and use efficient data structures when possible. Learn More: String Data Structure in Python: A Complete Case Study · Let’s explore some common string comparison scenarios and examples: To check if two strings are equal, use the ‘==’ operator or the ‘cmp()’ function.
🌐
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 - To do so, we can use the other comparison operators offered by Python. These are as follows: ... Let’s say we were creating a program that takes in two student names and returns a message with whose name comes first in the alphabet. We could use the following code to accomplish this task: student_one = "Penny" student_two = "Paul" if student_one > student_two: print("Penny comes before Paul in the alphabet.") elif student_one < student_two: print("Paul comes before Penny in the alphabet.")
🌐
Hostman
hostman.com › tutorials › how to compare python strings
Compare strings in Python: A Step-by-Step Guide | Hostman
December 24, 2025 - 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 code: first_string = input('Enter first string:\n') second_string = input('Enter the second line:\n') if first_string > second_string: print(f "In the dictionary, the sequence {first_string} is located after the sequence {second_string}") elif first_string < second_string: print(f "In the dictionary, the sequence {first_string} is located before the sequence {second_string}") else: print(f "The strings {first_string} and {second_string} are the same!")
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
W3Schools
w3schools.in › python › examples › compare-strings
Comparing Strings in Python
string1 = input("Enter a string: ") string2 = input("Enter another string: ") if string1 == string2: print("The strings are equal.") else: print("The strings are not equal.") The above python program will prompt the user to enter two strings and compare them using the == operator.
🌐
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}")
🌐
Delft Stack
delftstack.com › home › howto › python › if with string in python
if Statement With Strings in Python | Delft Stack
February 22, 2025 - Often, you’ll want to compare strings regardless of their case. Python doesn’t have a built-in case-insensitive comparison operator, but you can easily achieve this by converting strings to a common case before comparison.
🌐
Scaler
scaler.com › home › topics › string comparison in python
How to do String Comparison in Python?
February 15, 2024 - In Python, string comparison can also be performed using the is and is not operators, which are fundamentally different from the commonly used == and !=. These operators do not compare the content of the strings but rather their identities, ...
🌐
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 - Then, as mentioned in the code comment, we use the relational operators to compare the two strings. Here- First, we use the equality operator (==) to compare if x is equal to y. The result of this comparison (either True or False) is stored ...
🌐
IONOS
ionos.com › digital guide › websites › web development › how to compare strings in python
How to compare strings in Python - IONOS
November 21, 2023 - If you don’t want to type any more words, enter \'.\'') if temp == '.': break input_list.append(temp) print('Your entry: ', input_list) i = 0 alph = 1 while(i < len(input_list) - 1): if(input_liste[i] > input_list[i + 1]): print('This list is not sorted alphabetically!') alph = 0 break i = i + 1 if(alph == 1): print('This list is sorted alphabetically.')Python · In this more complex example, the user is asked to enter people’s surnames and first names.
🌐
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' == ...