is is identity testing, and == is equality testing. What happens in your code would be emulated in the interpreter like this:

>>> a = 'pub'
>>> b = ''.join(['p', 'u', 'b'])
>>> a == b
True
>>> a is b
False

So, no wonder they're not the same, right?

In other words: a is b is the equivalent of id(a) == id(b)

Answer from SilentGhost on Stack Overflow
Top answer
1 of 15
1698

is is identity testing, and == is equality testing. What happens in your code would be emulated in the interpreter like this:

>>> a = 'pub'
>>> b = ''.join(['p', 'u', 'b'])
>>> a == b
True
>>> a is b
False

So, no wonder they're not the same, right?

In other words: a is b is the equivalent of id(a) == id(b)

2 of 15
672

Other answers here are correct: is is used for identity comparison, while == is used for equality comparison. Since what you care about is equality (the two strings should contain the same characters), in this case the is operator is simply wrong and you should be using == instead.

The reason is works interactively is that (most) string literals are interned by default. From Wikipedia:

Interned strings speed up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on hash tables with string keys. Without interning, checking that two different strings are equal involves examining every character of both strings. This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fills up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.

So, when you have two string literals (words that are literally typed into your program source code, surrounded by quotation marks) in your program that have the same value, the Python compiler will automatically intern the strings, making them both stored at the same memory location. (Note that this doesn't always happen, and the rules for when this happens are quite convoluted, so please don't rely on this behavior in production code!)

Since in your interactive session both strings are actually stored in the same memory location, they have the same identity, so the is operator works as expected. But if you construct a string by some other method (even if that string contains exactly the same characters), then the string may be equal, but it is not the same string -- that is, it has a different identity, because it is stored in a different place in memory.

🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-comparison
Python Compare Strings: Methods, Operators & Best Practices | DigitalOcean
June 15, 2026 - If you want a refresher on string fundamentals first, see this article on working with strings in Python. If you work in more than one programming language, Python’s approach is simple: one operator, ==, compares string values, and there is no easy way to accidentally compare memory addresses when you meant to compare values.
Discussions

Comparing Strings
I am trying to program a Hangman ... but since I won’t be guessing the letters in the correct order, is there a way to compare the string without a simple ‘==’ that would check if the letters match instead of the entire string?... More on discuss.python.org
🌐 discuss.python.org
19
0
July 11, 2023
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
How to compare if a string value in one list is another list?
For best performance, try turning each list into a set, then perform set operations. Not only does it make it easier to comprehend, the methods are radically faster. (“ABC” in set is MUCH faster than “ABC” in list) In your case, you can use the set.intersection method and see if the result is empty. More on reddit.com
🌐 r/learnpython
8
2
March 28, 2022
string comparison
You didn't show your code. Python does not compare substrings as the same: "free" == "freedom" # False More on reddit.com
🌐 r/learnpython
6
0
September 10, 2021
🌐
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")

🌐
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 - Each character in a string has ... g has a value of of 103. When compared, g becomes greater than G. The == operator checks if two strings are equal....
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-comparison-in-python
String Comparison in Python - GeeksforGeeks
Python supports several operators ... with a simple example to illustrate these operators. ... The == operator is a simple way to check if two strings are identical....
Published   March 18, 2026
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-if.html
If and Comparisons
the comparison expression num == 6 evaluates to True when num is 6 and False otherwise. ... == test if two values are equal (2 equals signs together). Works for int, string, list, dict, ..
Find elsewhere
🌐
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…
🌐
ReqBin
reqbin.com › code › python › l1pjhl0c › python-compare-strings-example
How do I compare strings in Python?
July 27, 2023 - Python provides a rich set of methods for comparing strings. If you want to check for string equality, use the '==' or '!=' operators. If you want to compare strings as objects, use the 'is' and 'is not' operators.
🌐
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, com­par­i­son 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 prop­er­ties that can be compared.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-equals
How to Check if Two Strings Are Equal in Python (With Examples) | DigitalOcean
September 12, 2025 - The == operator checks if the values of two objects are equal, while the is operator checks if two objects are the same (i.e., they refer to the same memory location). ... s1 = 'Hello' s2 = 'Hello' print(s1 == s2) # Output: True print(s1 is s2) # Output: True, but this checks object identity, ...
🌐
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 - We begin the Python program above ... relational operators to compare the two strings. Here- First, we use the equality operator (==) to compare if x is equal to y....
🌐
WsCube Tech
wscubetech.com › resources › python › programs › string-comparison
Comparison of Strings in Python (6 Programs)
November 3, 2025 - Master string comparison in Python with 6 easy programs. Follow step-by-step examples with outputs and clear explanations. Read now!
🌐
Index.dev
index.dev › blog › python-string-comparison-methods
Python String Comparison: 8 Easy Ways You Must Know
May 15, 2025 - 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.") 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.
🌐
W3Schools
w3schools.in › python › examples › compare-strings
Comparing Strings in Python
string1 = "hello" string2 = "world" if string1 != string2: print("The strings are not equal.") else: print("The strings are equal.") The above python program will output "The strings are not equal." You can also use the < and > operators to compare the lexicographic order of the strings, meaning that you can compare the strings based on the order of their characters in the ASCII or Unicode table.
🌐
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 = ...
🌐
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 ...
🌐
Scaler
scaler.com › home › topics › string comparison in python
How to Compare Strings in Python? (With Examples) - Scaler Topics
February 15, 2024 - In the case of strings, relational operators sequentially compare each character of both strings according to the characters' Unicode value. As comparison is done for each character, relational operators maintain lexicographical order.
🌐
DEV Community
dev.to › itsmycode › python-compare-strings-a-step-by-step-guide-dae
Python Compare Strings: A Step-By-Step Guide - DEV Community
October 1, 2021 - The == ( equals ) and != ( not ... as with integer and float comparisons. The == (equals) operator returns true if the two string matches with each other otherwise it returns false....
🌐
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 - Strings in Python are compared with == and != operators. These compare if two Python strings are equivalent or not equivalent, respectively.