21043/comparing-strings-in-python-using-or-is
In the following Python code:
>>> s1 = 'Hello' >>> s2 = 'Hello' >>> s2 is s1 True
If I compare Var1 is Var2 in a conditional statement if fails to give the correct answer but if i write Var1 == Var2 instead, it returns true. Why is that happening?
is is used for identity testing and == is used for equality testing, i.e, is is the id(a) == id(b).
So your code will be emaluated in the following way:
>>> a = 'hi' >>> b = ''.join(['h', 'i']) >>> a == b True >>> a is b False
Convert both the strings to either uppercase ...READ MORE
In python objects/variables are wrapped into methods ...READ MORE
There is an efficient way to write ...READ MORE
For Python 3, try doing this: import urllib.request, ...READ MORE
Use , to separate strings and variables while printing: print ...READ MORE
For a better understanding you can refer ...READ MORE
suppose you have a string with a ...READ MORE
You can also use the random library's ...READ MORE
In this case, you can use the ...READ MORE
my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE
OR
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.