python中字符串的真值

时间:2022-09-26 16:53:55
if <boolean> :
   # do this

boolean has to be either True or False.

boolean必须是True或False。

then why

那么为什么

if "poi":
   print "yes"

output: yes

输出:是的

i didn't get why yes is printing , since "poi" is nether True or False.

我不知道为什么是打印,因为“poi”是虚假的真或假。

3 个解决方案

#1


17  

Python will do its best to evaluate the "truthiness" of an expression when a boolean value is needed from that expression.

当需要从该表达式获取布尔值时,Python将尽力评估表达式的“真实性”。

The rule for strings is that an empty string is considered False, a non-empty string is considered True. The same rule is imposed on other containers, so an empty dictionary or list is considered False, a dictionary or list with one or more entries is considered True.

字符串规则是空字符串被视为False,非空字符串被视为True。对其他容器施加相同的规则,因此空字典或列表被视为False,具有一个或多个条目的字典或列表被视为True。

The None object is also considered false.

None对象也被视为false。

A numerical value of 0 is considered false (although a string value of '0' is considered true).

数值0被认为是假的(尽管字符串值'0'被认为是真的)。

All other expressions are considered True.

所有其他表达式都被视为True。

Details (including how user-defined types can specify truthiness) can be found here: http://docs.python.org/release/2.5.2/lib/truth.html.

详细信息(包括用户定义的类型如何指定真实性)可以在这里找到:http://docs.python.org/release/2.5.2/lib/truth.html。

#2


8  

In python, any string except an empty string defaults to True

在python中,除空字符串之外的任何字符串都默认为True

ie,

if "MyString":
    # this will print foo
    print("foo")

if "":
    # this will NOT print foo
    print("foo")

#3


5  

What is happening here is Python' supplement of implicit bool() constructor after the if, Because anything followed by if should be resolved to be boolean. In this context your code is equivalent to

这里发生的是Python在if之后的隐式bool()构造函数的补充,因为后面跟着的if应该被解析为boolean。在这种情况下,您的代码等同于

if bool("poi"):
   print "yes"

According to Python bool(x) constructor accepts anything and decides the truthiness based on below cases

根据Python bool(x)构造函数接受任何内容并根据以下情况决定真实性

  • If x is integer, Only 0 is False everything else is True
  • 如果x是整数,则只有0为False,其他都为True
  • If x is float, Only 0.0 is False everything else is True`
  • 如果x是float,则只有0.0是False,其他一切都是True
  • If x is list, Only [] is False everything else is True
  • 如果x是list,则只有[]为False,其他一切都为True
  • If x is set/dict, Only {} is False everything else is True
  • 如果x设置为/ dict,则只有{}为False,其他一切都为True
  • If x is tuple, Only () is False everything else is True
  • 如果x是元组,则Only()为False,其他一切都为True
  • If x is string, Only “" is False everything else is True. Be aware that bool(“False”) will return to True
  • 如果x是string,则只有“”为False,其他一切都为True。请注意bool(“False”)将返回True

Here is the log for the cases I listed above

这是我上面列出的案例的日志

Python 3.4.3 (default, Feb 25 2015, 21:28:45) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> bool(0)
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0.0)
False
>>> bool(0.02)
True
>>> bool(-0.10)
True
>>> bool([])
False
>>> bool([1,2])
True
>>> bool(())
False
>>> bool(("Hello","World"))
True
>>> bool({})
False
>>> bool({1,2,3})
True
>>> bool({1:"One", 2:"Two"})
True
>>> bool("")
False
>>> bool("Hello")
True
>>> bool("False")
True

#1


17  

Python will do its best to evaluate the "truthiness" of an expression when a boolean value is needed from that expression.

当需要从该表达式获取布尔值时,Python将尽力评估表达式的“真实性”。

The rule for strings is that an empty string is considered False, a non-empty string is considered True. The same rule is imposed on other containers, so an empty dictionary or list is considered False, a dictionary or list with one or more entries is considered True.

字符串规则是空字符串被视为False,非空字符串被视为True。对其他容器施加相同的规则,因此空字典或列表被视为False,具有一个或多个条目的字典或列表被视为True。

The None object is also considered false.

None对象也被视为false。

A numerical value of 0 is considered false (although a string value of '0' is considered true).

数值0被认为是假的(尽管字符串值'0'被认为是真的)。

All other expressions are considered True.

所有其他表达式都被视为True。

Details (including how user-defined types can specify truthiness) can be found here: http://docs.python.org/release/2.5.2/lib/truth.html.

详细信息(包括用户定义的类型如何指定真实性)可以在这里找到:http://docs.python.org/release/2.5.2/lib/truth.html。

#2


8  

In python, any string except an empty string defaults to True

在python中,除空字符串之外的任何字符串都默认为True

ie,

if "MyString":
    # this will print foo
    print("foo")

if "":
    # this will NOT print foo
    print("foo")

#3


5  

What is happening here is Python' supplement of implicit bool() constructor after the if, Because anything followed by if should be resolved to be boolean. In this context your code is equivalent to

这里发生的是Python在if之后的隐式bool()构造函数的补充,因为后面跟着的if应该被解析为boolean。在这种情况下,您的代码等同于

if bool("poi"):
   print "yes"

According to Python bool(x) constructor accepts anything and decides the truthiness based on below cases

根据Python bool(x)构造函数接受任何内容并根据以下情况决定真实性

  • If x is integer, Only 0 is False everything else is True
  • 如果x是整数,则只有0为False,其他都为True
  • If x is float, Only 0.0 is False everything else is True`
  • 如果x是float,则只有0.0是False,其他一切都是True
  • If x is list, Only [] is False everything else is True
  • 如果x是list,则只有[]为False,其他一切都为True
  • If x is set/dict, Only {} is False everything else is True
  • 如果x设置为/ dict,则只有{}为False,其他一切都为True
  • If x is tuple, Only () is False everything else is True
  • 如果x是元组,则Only()为False,其他一切都为True
  • If x is string, Only “" is False everything else is True. Be aware that bool(“False”) will return to True
  • 如果x是string,则只有“”为False,其他一切都为True。请注意bool(“False”)将返回True

Here is the log for the cases I listed above

这是我上面列出的案例的日志

Python 3.4.3 (default, Feb 25 2015, 21:28:45) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> bool(0)
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0.0)
False
>>> bool(0.02)
True
>>> bool(-0.10)
True
>>> bool([])
False
>>> bool([1,2])
True
>>> bool(())
False
>>> bool(("Hello","World"))
True
>>> bool({})
False
>>> bool({1,2,3})
True
>>> bool({1:"One", 2:"Two"})
True
>>> bool("")
False
>>> bool("Hello")
True
>>> bool("False")
True