Python a-t-il une chaîne 39; maintient la méthode de sous-chaîne#39

Mots clés : pythonstringsubstringcontainspython

meilleur 4 Réponses Python a-t-il une chaîne 39; maintient la méthode de sous-chaîne#39

vote vote

100

if "blah" not in somestring:      continue 
vote vote

86

s = "This be a string" if s.find("is") == -1:     print("No 'is' here!") else:     print("Found 'is' in the string.") 
vote vote

74

'substring' in any_string 
start = 0 stop = len(any_string) any_string.find('substring', start, stop) 
start = 100  end = 1000 any_string.index('substring', start, end) 
>>> 'foo' in '**foo**' True 
>>> 'foo' not in '**foo**' # returns False False 
str.__contains__('**foo**', 'foo') 
'**foo**'.__contains__('foo') 
class NoisyString(str):     def __contains__(self, other):         print(f'testing if "{other}" in "{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 
>>> '**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 
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'))), } 
>>> 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} 
>>> from dis import dis >>> dis(lambda: 'a' in 'b')   1           0 LOAD_CONST               1 ('a')               2 LOAD_CONST               2 ('b')               4 COMPARE_OP               6 (in)               6 RETURN_VALUE >>> dis(lambda: 'b'.__contains__('a'))   1           0 LOAD_CONST               1 ('b')               2 LOAD_METHOD              0 (__contains__)               4 LOAD_CONST               2 ('a')               6 CALL_METHOD              1               8 RETURN_VALUE 
vote vote

66

>>> "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 >>> ["foo" in a for a in ["fo", "o", "foobar"]] [False, False, True] 

Questions similaires