Back
Does anyone know how to do the conversion from a string to a boolean in Python?
The reason I asked this is because I learned int("string"), from here. I tried bool("string") but always got True.
>>> bool("False") True
>>> bool("False")
True
You can use the following way to get the desired output:-
def str2bool(v): return str(v).lower() in ("yes", "true", "t", "1")str2bool("no")
def str2bool(v):
return str(v).lower() in ("yes", "true", "t", "1")
str2bool("no")
Interested to learn more about Python? Come & join our Python online course
31k questions
32.8k answers
501 comments
693 users