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

Python中的字符串查找示例

  1. import re
  2. text = 'This is sample text to test if this pythonic '\
  3. 'program can serve as an indexing platform for '\
  4. 'finding words in a paragraph. It can give '\
  5. 'values as to where the word is located with the '\
  6. 'different examples as stated'
  7. find_the_word = re.finditer('as', text)
  8. for match in find_the_word:
  9. print('start {}, end {}, search string \'{}\''.
  10. format(match.start(), match.end(), match.group()))

Output:

  1. start 63, end 65, search string 'as'
  2. start 140, end 142, search string 'as'
  3. start 200, end 202, search string 'as'

该分类下的相关小册推荐: