【发布时间】:2019-02-23 00:52:45
【问题描述】:
谁能解释一下为什么连接一个字符串和一个布尔值(由于使用了“in”关键字)会返回一个 False 值,而不是给出一个类型错误?
>>> var1 = "fish"
>>> var2 = "fi"
>>> print("True that " + var1 in var2) # str + bool returns False not TypeError
**False**
>>> print("True that ", var1 in var2) # str, bool works with comma method
True that False
>>> print("True that " + var2 in var1) # str + bool returns False not TypeError
**False**
>>> print("True that ",var2 in var1) # stre , bool works with comma method
True that True
>>> print(var2 in var1)
True
>>> type("True that ")
<class 'str'>
>>> type(var2 in var1)
<class 'bool'>
【问题讨论】:
标签: python string boolean concatenation