Python是否有一个string“包含”子string方法?
我在Python中寻找一个string.contains或string.indexof方法。 
我想要做:
 if not somestring.contains("blah"): continue 
	
 您可以使用in运算符 : 
 if "blah" not in somestring: continue 
 如果只是一个子stringsearch,你可以使用string.find("substring") 。 
 你必须要小心find , index ,尽pipe如此,因为他们是子stringsearch。 换句话说,这个: 
 s = "This be a string" if s.find("is") == -1: print "No 'is' here!" else: print "Found 'is' in the string." 
 它会打印Found 'is' in the string. 同样, if "is" in s:将评估为True 。 这可能是也可能不是你想要的。 
  if needle in haystack:堆里的if needle in haystack:是正常的使用,正如@迈克尔所说的那样 – 它依赖于in运算符,比方法调用更可读,更快。 
 如果你真的需要一个方法而不是操作符(例如,做一个奇怪的key=一个非常奇怪的types?),那就是'haystack'.__contains__ 。 但是因为你的例子是用在if ,我猜你并不是真正的意思,你说什么;-)。 这不是直接使用特殊方法的好forms(也不可读,也不是有效的),而是通过运营商和内部代理来使用它们。 
基本上,你想在Python中find一个string中的子string。 有两种方法可以在Python中searchstring中的子string。
  方法1: in运营商 
 您可以使用Python中的运算符来检查子string。 这非常简单直观。 如果在string中find子string,则返回True 。 
 >>> "King" in "King's landing" True >>> "Jon Snow" in "King's landing" False 
  方法2: str.find()方法 
 第二种方法是使用str.find()方法。 在这里,我们在要find子string的string上调用.find()方法。 我们将子string传递给find()方法并检查其返回值。 如果它的值不是-1,则在string中find子string,否则不能。 返回的值是find子string的索引。 
 >>> some_string = "valar morghulis" >>> some_string.find("morghulis") 6 >>> some_string.find("dohaeris") -1 
我会build议你使用第一种方法,因为它更加Pythonic和直觉。
 不,没有任何string.contains(str)方法,但有in运算符: 
 if substring in someString: print "It's there!!!" 
这是一个更复杂的工作示例:
 # Print all files with dot in home directory import commands (st, output) = commands.getstatusoutput('ls -a ~') print [f for f in output.split('\n') if '.' in f ] 
Python有一个包含子串方法的string吗?
 是的,但Python有一个比较运算符,您应该使用,因为该语言意图使用它,而其他程序员则希望您使用它。 该关键字in ,用作比较运算符: 
 >>> 'foo' in '**foo**' True 
 原来问题所要求的相反(补充) not in : 
 >>> 'foo' not in '**foo**' # returns False False 
 这在语义上与not 'foo' in '**foo**'相同,但是在语言中作为可读性改进,其更易于阅读和明确提供。 
 避免使用__contains__ , find和index 
 如承诺,这里是contains方法: 
 str.__contains__('**foo**', 'foo') 
 返回True 。 你也可以从超级string的实例中调用这个函数: 
 '**foo**'.__contains__('foo') 
 但是不要。 以下划线开头的方法被认为是语义上的私有的。 使用这个的唯一原因是扩展in和not infunction(例如,如果子类str ): 
 class NoisyString(str): def __contains__(self, other): print('testing if "{0}" in "{1}"'.format(other, self)) return super(NoisyString, self).__contains__(other) ns = NoisyString('a string with a substring inside') 
现在:
 >>> 'substring' in ns testing if "substring" in "a string with a substring inside" True 
另外,避免以下string方法:
 >>> '**foo**'.index('foo') 2 >>> '**foo**'.find('foo') 2 >>> '**oo**'.find('foo') -1 >>> '**oo**'.index('foo') Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> '**oo**'.index('foo') ValueError: substring not found 
 其他语言可能没有直接testing子string的方法,所以您将不得不使用这些types的方法,但是使用Python,使用in比较运算符会更有效率。 
性能比较
我们可以比较各种实现相同目标的方式。
 import timeit def in_(s, other): return other in s def contains(s, other): return s.__contains__(other) def find(s, other): return s.find(other) != -1 def index(s, other): try: s.index(other) except ValueError: return False else: return True perf_dict = { 'in:True': min(timeit.repeat(lambda: in_('superstring', 'str'))), 'in:False': min(timeit.repeat(lambda: in_('superstring', 'not'))), '__contains__:True': min(timeit.repeat(lambda: contains('superstring', 'str'))), '__contains__:False': min(timeit.repeat(lambda: contains('superstring', 'not'))), 'find:True': min(timeit.repeat(lambda: find('superstring', 'str'))), 'find:False': min(timeit.repeat(lambda: find('superstring', 'not'))), 'index:True': min(timeit.repeat(lambda: index('superstring', 'str'))), 'index:False': min(timeit.repeat(lambda: index('superstring', 'not'))), } 
 现在我们看到,使用in比其他的更快。 做同等操作的时间更less: 
 >>> perf_dict {'in:True': 0.16450627865128808, 'in:False': 0.1609668098178645, '__contains__:True': 0.24355481654697542, '__contains__:False': 0.24382793854783813, 'find:True': 0.3067379407923454, 'find:False': 0.29860888058124146, 'index:True': 0.29647137792585454, 'index:False': 0.5502287584545229} 
  in Pythonstring和列表中 
这里有几个有用的例子,说明了自己的方法:
 "foo" in "foobar" True "foo" in "Foobar" False "foo" in "Foobar".lower() True "foo".capitalize() in "Foobar" True "foo" in ["bar", "foo", "foobar"] True "foo" in ["fo", "o", "foobar"] False 
 警告。 列表是可迭代的, in方法作用于迭代,而不仅仅是string。 
所以显然没有什么类似的vector明智的比较。 一个明显的Python方法是:
 names = ['bob', 'john', 'mike'] any(st in 'bob and john' for st in names) >> True any(st in 'mary and jane' for st in names) >> False 
 另一种方法来查找一个string是否包含几个字符或不带有布尔返回值(即True或False): 
 str1 = "This be a string" find_this = "tr" if find_this in str1: print find_this, " is been found in ", str1 else: print find_this, " is not found in ", str1 
在Python中,有两个简单的方法可以实现这一点:
Pythonic的方式:使用Python的'in'Keyword-
  in左边( 子string )和右边一个in有两个“参数”,如果左侧参数包含在权限参数中,则返回False ,否则返回False 。 
 example_string = "This is an example string" substring = "example" print(substring in example_string) 
输出:
 True 
非pythonic方式:使用Python的str.find:
  find方法返回string中的string位置,如果find返回-1。 但只要检查位置是不是-1。 
 if example_string.find(substring) != -1: print('Substring found!') else: print('Substring not found!') 
输出:
 Substring found! 
这是你的答案:
 if "insert_char_or_string_here" in "insert_string_to_search_here": //DOSTUFF 
为了检查它是否是错误的:
 if not "insert_char_or_string_here" in "insert_string_to_search_here": //DOSTUFF 
要么:
 if "insert_char_or_string_here" not in "insert_string_to_search_here": //DOSTUFF