python的string對象沒有contains方法,不用使用string.contains的方法判斷是否包含子字符串,但是python有更簡單的方法來替換contains函數。
方法1:使用 in 方法實現contains的功能:
site = 'http://www.sharejs.com/'if "sharejs" in site: 輸出結果:site contains sharejs方法2:使用find函數實現contains的功能。
s = "This be a string"if s.find("is") == -1: print "No 'is' here!"else: print "Found 'is' in the string."輸出結果:No ‘is’ here!
新聞熱點
疑難解答