-1
一种方法来检查,如果字符串中包含文本:如何找到一个字符串出现在另一个字符串
text = 'somewhere over the rainbow'
keyword = 'Rainbow'
if keyword.lower() in str(text).lower():
print 'yes, it is'
有一个较短的方法吗?
一种方法来检查,如果字符串中包含文本:如何找到一个字符串出现在另一个字符串
text = 'somewhere over the rainbow'
keyword = 'Rainbow'
if keyword.lower() in str(text).lower():
print 'yes, it is'
有一个较短的方法吗?
可以使用find()方法:
if text.lower().find(keyword.lower()) != -1:
print 'yes, it is'
你不需要'STR(文字)'。 'text'已经是一个字符串。 –
只要'text.lower():...'中的keyword.lower()就足够了。 –
如果你想探索字符串发生的更多高级版本https://stackoverflow.com/questions/10383044/fuzzy-string-comparison –