當前位置:才華齋>設計>網頁設計>

python判斷字串是否包含子字串的方法

網頁設計 閱讀(2.22W)

文章主要介紹了python判斷字串是否包含子字串的方法,例項分析了Python中的in與find方法來實現這一功能,非常具有實用價值,需要的'朋友可以參考下.

python判斷字串是否包含子字串的方法

本文例項講述了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程式設計有所幫助。