我将一个空值与一个数组元素匹配,该数组元素中包含一个值,并且该数组没有任何空值。但是,这个脚本仍然有用。
import re
b = ""
links_check_arr = ['Arunsdsdds']
for links_find in links_check_arr:
if b in links_find:
print links_find
print b
当b
为空且数组元素为'Arunsdsdds'
(其中没有空值)时,它是如何工作的?
答案 0 :(得分:3)
空字符串始终被视为任何字符串的一部分:
>>> "" in "abcd"
True
答案 1 :(得分:1)
空字符串是Python中每个字符串的子字符串。这是因为任何字符串s[0:0]
的长度为0的子字符串都等于空字符串。
检查字符串s
是否为空的正确方法只是if not s:
,因为空字符串是" falsey"。
要检查空字符串是否等于,只需使用==
而不是in
。
答案 2 :(得分:0)
tristan在帖子Why Python returns True when checking if an empty string is in another?中给出了详细的答案。重要的一句是:
空字符串始终被视为任何其他字符串的子字符串 字符串,所以“”在“abc”中将返回True。
你在Python 2.7中找到这句话。参考文章 Comparisons 一章中的文章Expressions。或者Expressions, chapter Membership test operations中的Python 3。