Did you know that the below code returns
That's because
1- s == 'True'
Do not use this piece of code if you don't know what you're doing. Misusing this technic puts a mess in your code! Be cautious.
2- The other more elegant way is to use
#python #boolean #true #false #cast #convert #bool
True
?>>> bool("False")
True
That's because
False
is a string and boolean of non-empty string is True
. There are workarounds:1- s == 'True'
Do not use this piece of code if you don't know what you're doing. Misusing this technic puts a mess in your code! Be cautious.
2- The other more elegant way is to use
json
library like below:>>> import json
>>> json.loads("false".lower())
False
>>> json.loads("True".lower())
True
NOTE:
you need to make it lower case in order to turn the string into a boolean value.#python #boolean #true #false #cast #convert #bool