为什么 bool("0") 在 python 中为真?为什么会这样?

Why bool("0") is true in python? why does this happen?

因为我最近开始学习 Python,我遇到了这个布尔概念并执行了这个表达式 bool("0"),但是我得到了一个令人困惑的结果,因为这个表达式的 True,谁能告诉我为什么会这样。

bool("0") 计算结果为 True 因为 "0" 在这种情况下是一个 non-empty 字符串。它对 :

这样的事情很有用
if str: #check if str is not empty
    #do something
另一方面,

bool(0) 的计算结果为 False。

这是因为它将 return 对每个 non-empty 字符串、列表等都为真

如果 int 类型变量为零,解释器将其显示为 False .bool("0") 计算结果为 True,因为在这种情况下“0”是一个 non-empty 字符串,而不是 intSee it full in one photo