Skip to content
Fix Code Error

Why does comparing strings using either ‘==’ or ‘is’ sometimes produce a different result?

March 13, 2021 by Code Error
Posted By: Anonymous

I’ve got a Python program where two variables are set to the value 'public'. In a conditional expression I have the comparison var1 is var2 which fails, but if I change it to var1 == var2 it returns True.

Now if I open my Python interpreter and do the same “is” comparison, it succeeds.

>>> s1 = 'public'
>>> s2 = 'public'
>>> s2 is s1
True

What am I missing here?

Solution

is is identity testing, == 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)

Answered By: Anonymous

Related Articles

  • Attaching textarea by v-model to computed with other…
  • How would I access variables from one class to another?
  • The null object does not have a method []=
  • VueJS: Cannot read property 'name' of undefined"
  • Maven2: Missing artifact but jars are in place
  • How to make Nuxt global object?
  • Java Can't connect to X11 window server using…
  • What are the new features in C++17?
  • Split (explode) pandas dataframe string entry to…
  • What's the difference between eval, exec, and compile?
  • MySQL: @variable vs. variable. What's the difference?
  • Form field border-radius is not working only on the…
  • Multiple left-hand assignment with JavaScript
  • C# group by a list contains another class
  • Reference — What does this symbol mean in PHP?
  • Can a "User Assigned Managed Identity" be used locally?
  • OpenIddict Roles/Policy returns 403 Forbidden
  • Activating a dart app with pub global activate
  • How should a model be structured in MVC?
  • Reduce vs Collect method on Parallel Streams vs…
  • Python 3: UnboundLocalError: local variable…
  • How to remove last n characters from a string in Bash?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Can't install via pip because of egg_info error
  • data.table: recode several variables without having…
  • Multiple returns from a function
  • Circular Dependency with two depending Services
  • What is a NullReferenceException, and how do I fix it?
  • Equals(=) vs. LIKE
  • C++ Compare char array with string
  • Is there some way to test my grammar which refer to…
  • What are the undocumented features and limitations…
  • SSH Key: “Permissions 0644 for 'id_rsa.pub' are too…
  • What is an optional value in Swift?
  • What does if __name__ == "__main__": do?
  • AngularJS ui-router login authentication
  • R apply function with multiple parameters
  • Why does this Azure Resource Manager Template fail…
  • How do I merge two dictionaries in a single…
  • Terraform: Call to function "element" failed: cannot…
  • AHK How to pass variables to "Run" inside a function?
  • ASP.NET MVC 5 - Identity. How to get current ApplicationUser
  • What is the difference between Scope_Identity(),…
  • Cordova: How to move file to the Download folder?
  • XPath - Difference between node() and text()
  • What does this symbol mean in JavaScript?
  • What are all the user accounts for IIS/ASP.NET and…
  • Makefile ifeq logical or
  • Comparing boxed Long values 127 and 128
  • Reference - What does this regex mean?
  • ValueError from Function
  • How to not show on the terminal my program
  • Downloading Java JDK on Linux via wget is shown…
  • Smart way to truncate long strings
  • Comparison sign as variable
  • Bash script shell input variables
  • An explicit value for the identity column in table…
  • How to get rid of this problem ( On adding a nuget…
  • Best way to compare two complex objects
  • How do the PHP equality (== double equals) and…
  • Evaluate empty or null JSTL c tags
  • Using the && operator in an if statement
  • ng-options with simple array init
  • Zero cost builder pattern for recursive data…
  • Rust: implementing an iterator from a vector of…
  • How to generate a random string of a fixed length in Go?
  • How to combine the data from two different…
  • Checkout another branch when there are uncommitted…
  • How to find out client ID of component for ajax…
  • Memcached vs. Redis?
  • Getting the closest string match
  • How to end C++ code
  • Does Python have a ternary conditional operator?
  • How to test Dart Polymer elements using the new Test…
  • What's the difference between equal?, eql?, ===, and ==?
  • Which equals operator (== vs ===) should be used in…
  • How to update Identity Column in SQL Server?
  • Elegant ways to support equivalence ("equality") in…
  • how concatenate two variables in batch script?
  • How to split one string into multiple variables in…
  • comparing two strings in ruby
  • Why does C++ code for testing the Collatz conjecture…
  • Nesting components with mithril.js
  • Running Python on Windows for Node.js dependencies
  • Using less in a polymer-dart application
  • Adding an identity to an existing column
  • Setting a global PowerShell variable from a function…
  • shell script. how to extract string using regular…
  • JWT authentication for ASP.NET Web API
  • How to prevent scrolling the whole page?
  • Error Message : Cannot find or open the PDB file
  • Vue.JS & Spring Boot - Redirect to homepage on 404
  • Why isn't my mixed model loop working? (RStudio,…
  • getting error while updating Composer
  • How do I return the response from an asynchronous call?
  • How to update Python?
  • PHP parse/syntax errors; and how to solve them
  • Trying to embed newline in a variable in bash
  • JavaScript checking for null vs. undefined and…
  • How do I use shell variables in an awk script?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

JavaScript function in href vs. onclick

Next Post:

How to randomly select an item from a list?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error