String comparison in Python is vs.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python has two comparison operators that beginners often confuse: == checks value equality (do two objects have the same content?), while is checks identity (are two variables the same object in memory?). For strings, == is almost always the correct choice. Using is for string comparison can appear to work due to Python's string interning optimization, but it is unreliable and can produce bugs that only appear in certain contexts.
The Difference: == vs is
When They Diverge
Why is Sometimes Works for Strings
Python interns (caches and reuses) certain strings to save memory:
Interning Rules (CPython)
CPython interns strings that:
- Are compile-time constants
- Contain only ASCII letters, digits, and underscores
- Are used as variable names, function names, or attributes
- Are explicitly interned with
sys.intern()
These rules are implementation details and may differ in PyPy, Jython, or future Python versions.
When to Use Each
| Operator | Use for | Example |
== | Comparing string content | if name == "admin": |
is | Checking for None | if x is None: |
is | Checking for sentinel objects | if result is MISSING: |
is not | Checking not None | if x is not None: |
The id() Function
id() returns the memory address of an object. a is b is equivalent to id(a) == id(b):
Explicit String Interning
Use sys.intern() to force interning for performance-critical lookups:
sys.intern() is useful when you have millions of repeated strings (like column names, tags, or tokens) and want to save memory and speed up comparisons.
Common String Comparison Operations
Python Linting Warnings
Modern linters warn about using is with string literals:
Starting with Python 3.8, using is or is not with string, integer, or float literals produces a SyntaxWarning.
Common Pitfalls
- Relying on
isfor strings:ismay work for short, simple strings due to interning, but fail for dynamically constructed strings. Always use==for content comparison. - Integer interning confusion: Like strings, Python caches small integers (-5 to 256).
a = 256; b = 256; a is bis True, buta = 257; b = 257; a is bis False. Same trap as string interning. - Mutable objects and
is: For lists, dicts, and other mutable objects,ischecks if they are the same instance:a = [1]; b = [1]; a == bis True buta is bis False. This is the correct behavior. is Noneis correct:Noneis a singleton — there is only oneNoneobject in Python. Usingis Noneis correct and recommended by PEP 8.- Performance myth: While
isis slightly faster than==(identity check vs value comparison), the difference is negligible. Do not useisfor performance — use it only for identity checks.
Summary
- Use
==to compare string values — this is the correct operator for content comparison - Use
isonly for identity checks:None, sentinel objects, and singleton patterns - String interning makes
isappear to work for some strings, but this is an implementation detail - Python 3.8+ warns when using
iswith literals:SyntaxWarning: "is" with a literal - Use
sys.intern()if you explicitly need interned strings for performance in high-volume scenarios
Related reading
- String Distance Matrix in Python
- String formatting % vs. .format vs. f-string literal
- String formatting vs. .format vs. f-string literal
- String similarity metrics in Python
- String to Dictionary in Python
- Strip HTML from strings in Python
- Stripping everything but alphanumeric chars from a string in Python
- str.startswith with a list of strings to test for
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.