使用正则表达式,您可以
re.finditer用来查找所有(不重叠)事件:
>>> import re>>> text = 'Allowed Hello Hollow'>>> for m in re.finditer('ll', text): print('ll found', m.start(), m.end())ll found 1 3ll found 10 12ll found 16 18另外,如果您不希望使用正则表达式,也可以重复使用
str.find来获取
下一个 索引:
>>> text = 'Allowed Hello Hollow'>>> index = 0>>> while index < len(text): index = text.find('ll', index) if index == -1: break print('ll found at', index) index += 2 # +2 because len('ll') == 2ll found at 1ll found at 10ll found at 16这也适用于列表和其他序列。



