- None值或者NoneType的值不能用in判断是否在一个字符串
>> tail = None
>> if tail in "fa":
print("fa")
Traceback (most recent call last):
File "/Users/duhuifang/git_local/guzhuan/NeeqExtractor/py35env/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-27-d9a296b9d386>", line 1, in <module>
if tail in "fa":
TypeError: 'in <string>' requires string as left operand, not NoneType
由上可知,在使用tail in "fa"
之前一定要要求tail != None
,另外注意空字符一定存在于任意字符串(包括空字符串)中,如下
>> head = ''
>> if head in "fa":
print("head 在 'fa'中")
head 在 'fa'中
>> null_str = ''
>> f head in null_str:
print("head 在 null_str中")
head 在 null_str中
- None 可以和一个变量用等于号或者不等于号来判断是否是同一个值:
>> value = None
>> tail = "haha"
>> if value != tail:
print("可以判断")
可以判断