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
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-comparison-in-python
String Comparison in Python - GeeksforGeeks
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.
Published: March 18, 2026
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-comparison
Python Compare Strings: Methods, Operators & Best Practices | DigitalOcean
Learn how to compare strings in Python using ==, is, and ordering operators, plus casefold() and difflib for tricky cases.
Discussions

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 - Why does comparing strings using either '==' or 'is' sometimes produce a different result? - Stack Overflow
With interned strings, a simple ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
not equals operator(!=) not working in python string comparison - Stack Overflow
I am trying to compare two strings in python 3.6 and if they are not equal then print a message and exit. My current code is: location = 'United States of America' if location.lower() != 'united s... More on stackoverflow.com
🌐 stackoverflow.com
comparison - Is there a "not equal" operator in Python? - Stack Overflow
How would you say "does not equal"? if hi == hi: print "hi" elif hi (does not equal) bye: print "no hi" Is there something similar to == that means "not ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 › python-not-equal-operator
Python NOT EQUAL operator - GeeksforGeeks
July 23, 2025 - The NOT EQUAL operator can also be used with the Python if else statements. Let us see a simple example of this. In this example, we are comparing two strings and then printing a message based on the output of does not equal operator in Python.
🌐
AskPython
askpython.com › python › string › string-equals-check-in-python
String Equals Check in Python: Using 4 Different Methods - AskPython
May 24, 2023 - Its return value is the opposite of the return value obtained using the ‘=’ operator. The ‘!=’ operator compares two strings and returns True if the strings are unequal, otherwise, it returns False.
Top answer
1 of 15
1700

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.

Find elsewhere
🌐
Squash
squash.io › string-comparison-in-python-best-practices-and-techniques
String Comparison in Python: Best Practices and Techniques
May 21, 2024 - # -*- coding: utf-8 -*- # String comparison using unicode in Python # Example strings with unicode characters string1 = "Café" string2 = "Cafe\u0301" # Method 1: Using the unicode normalization method import unicodedata # Normalize strings using NFKC normalization form normalized_string1 = unicodedata.normalize("NFKC", string1) normalized_string2 = unicodedata.normalize("NFKC", string2) # Compare normalized strings if normalized_string1 == normalized_string2: print("Method 1: Strings are equal") else: print("Method 1: Strings are not equal") # Method 2: Using the unicode collation method impo
🌐
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.
🌐
Replit
replit.com › home › discover › how to compare strings in python
How to compare strings in Python | Replit
April 14, 2026 - ... a = "hello" b = "hel" + "lo" if a == b: # '==' checks string content equality print("Strings are the same") else: print("Strings are different") The solution is to replace is with the == operator.
🌐
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 - Python comparison operators can be used to compare strings in Python. These operators are: equal to (==), not equal to (!=), greater than (>), less than (<), less than or equal to (<=), and greater than or equal to (>=).
🌐
FavTutor
favtutor.com › blogs › compare-strings-python
How to Compare String in Python? (String Comparison 101)
November 9, 2023 - It is a magic function defined in the string class and compares two strings to return True if they are equal or Fale if they are not. ... s1 = 'String' s2 = 'String' s3 = 'string' # case sensitive equals check if s1 == s2: print('s1 and s2 are equal.') if s1.__eq__(s2): print('s1 and s2 are equal.') Here, we check strings s1 and s2 whether are equal or not and then use the “if” conditional statement with a combination of the equal operator. ... In Python, the is operator is used to determine if two objects are the same object in memory.
🌐
W3Schools
w3schools.in › python › examples › compare-strings
Python Program to Compare Strings - W3schools
If the strings are different, the comparison returns False. ... string1 = "hello" string2 = "world" if string1 == string2: print("The strings are equal.") else: print("The strings are not equal.") The above python program will output "The strings are not equal." because the two strings are ...
🌐
Completeera
answers.completeera.com › post › how-to-check-if-strings-are-not-equal-in-python-simple-comparisons
Completeera
May 15, 2026 - Python makes this easy with simple syntax, but understanding the nuances ensures you write robust code. Let’s dive into the best ways to do it! ... The simplest way to check if two strings are not equal is by using the inequality operator !=. This operator returns True if the strings differ ...
🌐
Flexiple
flexiple.com › python › python-string-comparison
Various methods for Python String Comparison - Flexiple Tutorials - Flexiple
March 10, 2022 - The != is another python string comparison operator that checks if the values of the operands are not equal. It performs the opposite of == operator.
🌐
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.
🌐
AskPython
askpython.com › home › string comparison in python
String Comparison in Python - AskPython
August 6, 2022 - The following are the ways to compare two string in Python: By using == (equal to) operator · By using != (not equal to) operator · By using sorted() method · By using is operator · By using Comparison operators ·
🌐
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.