+ 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

13th Jul 2022, 8:22 PM
Sator Arepo🌾🌌
Sator Arepo🌾🌌 - avatar
2 Antworten
+ 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)
13th Jul 2022, 8:28 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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
14th Jul 2022, 7:21 PM
Emerson Prado
Emerson Prado - avatar