2017-10-08 132 views
-1

一種方法來檢查,如果字符串中包含文本:如何找到一個字符串出現在另一個字符串

text = 'somewhere over the rainbow' 
keyword = 'Rainbow' 

if keyword.lower() in str(text).lower(): 
    print 'yes, it is' 

有一個較短的方法嗎?

+3

你不需要'STR(文字)'。 'text'已經是一個字符串。 –

+2

只要'text.lower():...'中的keyword.lower()就足夠了。 –

+0

如果你想探索字符串發生的更多高級版本https://stackoverflow.com/questions/10383044/fuzzy-string-comparison –

回答

-1

可以使用find()方法:

if text.lower().find(keyword.lower()) != -1: 
    print 'yes, it is' 
相關問題