如何在 python 中将字符串转换为布尔值?

How can I convert String to Boolean in python?

我需要从字符串“True and False”中获取值 True 和 False。

可以用eval函数测试条件,然后用str函数返回:

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