+ 4
Is an empty string true or false in python 3
This is the code I have made to check it but I don't get it still... https://code.sololearn.com/cAV8IGGRwinU/?ref=app When I check that ''==False it returns false. But if I use bool('') it returns False....
12 ответов
+ 6
7.0 and 7 have different types too, yet they're equal. The point is that '' and False have the same boolean values, but that doesn't make them equal. None, [], set() etc. are all False. If we were to say that '' == False were True, we'd consequently have to say that 0 == '', [] == None etc. are also True because if a=b and b=c, then a=c (btw, 5 and 7 also have the same boolean values but that doesn't mean that 5 == 7 should be True)
+ 5
Saksham Jain Both ''==False and '' ==True are False because an empty string is not a boolean expression. It has a boolean value which can be found out with bool(''). But an empty string is not equivalent to False. If it were the same, an expression like if 5-5=='' would evaluate to True. 5-5 is 0, 0 is False. '' is False. False == False is True.
This expression would also be True: 'hello' == 'goodbye' (True == True is True). Using boolean logic this way obviously leads to unwanted results
+ 4
Anna I still not got why
''==False is False
And also ''==True is False
But bool('')==False is True.
+ 2
Aymane Boukrouh that I can see from my code but why doesn't ""==False give True?
+ 2
Aymane Boukrouh I wanted ask why doesn't the second if block executed in my code
+ 1
Saksham Jain I think an empty string is neither false nor true. If you try:
if(not None):
print('test')
Then you get test.
So probably not None ans not empty_string returns True
+ 1
Try if bool("") == False
+ 1
It's like comparing apples with pears, the conditions: "==False or "==None or "==[] or "=={}, will never be True because are different types
0
False
0
Try:
a = ''
If(not a):
print("test")
OUTPUT: test
0
I don't know to be honest, I'll look for it and answer you.
0
you are using if function, if the result of if function is some value the it will say "True" , otherwise if it return zero or blank, it will return boolean "False"