+ 2
String booleans
I don't understand if any non empty string is True, then why a comparison ('string' == True) is False https://code.sololearn.com/cWrP6q6mcR9D/?ref=app
2 Answers
+ 6
Hi Sator Arepoš¾š
Thatās because you are comparing to different datatypes; a string with a boolean. But you can test this:
print(bool('string') == True)
+ 1
A non-empty string evaluates to True only when treated as a boolean, like:
if 'something': # Evals to True
When used as is, it keeps its value:
if 'True' == True # Evals to False