测试字符串是否包含或以#34; no-value" ("&#34)

时间:2018-03-01 16:25:16

标签: python

任何人都可以解释为什么在测试时字符串中是否包含无值("")或字符串startswith无值("" ),返回值为True 如:

>>> if "" in "L": print ("YES")
... 
YES
>>> if "" in " ": print ("YES")
... 
YES
>>> if "" in "abc": print ("YES")
... 
YES
>>> if "abc".startswith(""): print (True)
... 
True

https://docs.python.org/3/library/stdtypes.html#truth-value-testing
4.6.1。常用序列操作

  

x in s如果s的项等于x,则为True,否则为False

4.7.1。字符串方法

  

str.startswith(prefix [,start [,end]])如果字符串开始则返回True   使用前缀,否则返回False。

我真的想知道我是否可以依赖这种行为 它使用以下代码:

sub_choices = [c for c in self.choices if Input_Text.casefold() in c.casefold()]

sub_choices = [c for c in self.choices if c.casefold().startswith(Input_Text.casefold())]

在这种情况下如果Input_Text为空,则sub_choices(由于意外发生)是self.choices的全部内容

0 个答案:

没有答案
相关问题