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

在 Python 中查找给定字符串中的整个单词

  1. def contains_word(s, w):
  2. return (' ' + w + ' ') in (' ' + s + ' ')
  3. result = contains_word('those who seek shall find', 'find')
  4. print(result)
  5. result = contains_word('those who seek shall find', 'finds')
  6. print(result)

Output:

  1. True
  2. False

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