【问题标题】:How can I convert String to Boolean in python? [closed]如何在 python 中将字符串转换为布尔值? [关闭]
【发布时间】:2020-03-12 19:24:11
【问题描述】:

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

【问题讨论】:

  • 请附上您尝试过的代码,并添加一些有关您正在使用的语言或工具的信息,以便这篇文章符合帮助中心提出问题的指南。 stackoverflow.com/help/asking
  • 语言是python,希望现在问题清楚了

标签: python string boolean return-value transformation


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2011-02-17
    • 2012-04-02
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多