当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

在Python中查找字符串中所有出现的单词的所有索引

  1. import re
  2. sentence = 'this is a sentence this this'
  3. word = 'this'
  4. for match in re.finditer(word, sentence):
  5. print(match.start(), match.end())

Output:

  1. 0 4
  2. 19 23
  3. 24 28