Python 如何在字符串中搜索单词
我试图在字符串中搜索一个单词 范例Python 如何在字符串中搜索单词,python,Python,我试图在字符串中搜索一个单词 范例 match = 'File' s2 = 'name of File: is .jpg' if match not in s2: print 'ok' 它成功了。我可以用列表执行此操作吗 match = ['File','Category'] 你当然可以。因此,我们检查match中的每个单词是否在s2中 for word in match: if word not in s2: print 'ok' 或一个简单的单内衬- [w
match = 'File'
s2 = 'name of File: is .jpg'
if match not in s2:
print 'ok'
它成功了。我可以用列表
执行此操作吗
match = ['File','Category']
你当然可以。因此,我们检查
match
中的每个单词是否在s2
中
for word in match:
if word not in s2:
print 'ok'
或一个简单的单内衬-
[word for word in match if word not in s2]
你当然可以。因此,我们检查
match
中的每个单词是否在s2
中
for word in match:
if word not in s2:
print 'ok'
或一个简单的单内衬-
[word for word in match if word not in s2]
如果你只是在寻找一个词的存在:
>>> 'St' in 'Stack'
True
如果您也在寻找其职位:
>>> ("stack").find("st")
0
注意:下面的内容是从中提取的,而上面的内容是我的原创作品,仅供参考:
语法:
str.find(sub[, start[, end]])
应返回找到子字符串sub的字符串中的最低索引,以便sub包含在切片s[start:end]中。可选参数start和end解释为切片表示法。如果未找到子项,则返回-1
参考资料:
str.find(sub[, start[, end]])
如果你只是在寻找一个词的存在:
>>> 'St' in 'Stack'
True
如果您也在寻找其职位:
>>> ("stack").find("st")
0
注意:下面的内容是从中提取的,而上面的内容是我的原创作品,仅供参考:
语法:
str.find(sub[, start[, end]])
应返回找到子字符串sub的字符串中的最低索引,以便sub包含在切片s[start:end]中。可选参数start和end解释为切片表示法。如果未找到子项,则返回-1
参考资料:
str.find(sub[, start[, end]])
是否要搜索准确的单词?也就是说,如果你的搜索词是
foo
,你希望它与foobar
中的foo
匹配吗?@Tim Pietzcker不是精确的词。你想搜索精确的词吗?也就是说,如果你的搜索词是foo
,你希望它与foobar
中的foo
匹配吗?@Tim Pietzcker不是精确的词。