python判斷字符串是否包含子字符串的方法
來源:文萃谷 2.13W
文章主要介紹了Python判斷字符串是否包含子字符串的方法,實例分析了Python中的in與find方法來實現這一功能,非常具有實用價值,需要的'朋友可以參考下.
本文實例講述了python判斷字符串是否包含子字符串的方法。分享給大家供大家參考。具體如下:
python的string對象沒有contains方法,不用使用ains的方法判斷是否包含子字符串,但是python有更簡單的方法來替換contains函數。
方法1:使用 in 方法實現contains的功能:
?
1
2
3
site = ''
if "jb51" in site:
print('site contains jb51')
輸出結果:site contains jb51
方法2:使用find函數實現contains的功能
?
1
2
3
4
5
s = "This be a string"
if ("is") == -1:
print "No 'is' here!"
else:
print "Found 'is' in the string."
希望本文所述對大家的Python程序設計有所幫助。