Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (33.1k points)

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

1 Answer

0 votes
by (106k points)

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")

image

Interested to learn more about Python? Come & join our Python online course

Related questions

+3 votes
2 answers
0 votes
1 answer
asked Sep 17, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...