-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 –