[SOLVED] How can I convert String to Boolean in python?


I need to get the value True and False from the string "True and False".


You can use the eval function to test the condition and then the str function to go back:

>>> condition = 'True and False'
>>> test = eval(condition)
>>> test
>>> type(test)
<class 'bool'>
>>> result = str(test)
>>> result
>>> type(result)
<class 'str'>

Answered By – Philippe Dixon

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *