如何在Python中检查一个子字符串是否包含在另一个字符串中
Python有一个关键字’in’,用于查找一个字符串是否是另一个字符串的子字符串。例如
print('ello' in 'hello world')
输出
True
如果您还需要子字符串的第一个索引,可以使用find(substr)查找索引。如果此方法返回-1,则表示字符串中不存在子字符串。例如,
print("hello world".find('ello'))
输出
1
检查字符串’Harry Potter: The Goblet of Fire’是否包含’no’
print("Harry Potter: The Goblet of Fire".find('no'))
输出
-1
极客教程