- 相關推薦
python判斷字符串是否包含子字符串的方法
文章主要介紹了python判斷字符串是否包含子字符串的方法,實例分析了Python中的in與find方法來實現這一功能,非常具有實用價值,需要的朋友可以參考下.
本文實例講述了python判斷字符串是否包含子字符串的方法。分享給大家供大家參考。具體如下:
python的string對象沒有contains方法,不用使用string.contains的方法判斷是否包含子字符串,但是python有更簡單的方法來替換contains函數。
方法1:使用 in 方法實現contains的功能:
?
1
2
3
site = 'http://www.jb51.net/'
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 s.find("is") == -1:
print "No 'is' here!"
else:
print "Found 'is' in the string."
希望本文所述對大家的Python程序設計有所幫助。
【python判斷字符串是否包含子字符串的方法】相關文章:
java判斷字符串是否為數字的幾個方法07-26
java常用字符串方法03-25
Java字符串排序中文和數字的方法12-14
PHP字符串操作05-03
Java字符串(String)05-18
PHP的字符串函數02-18
C語言字符串07-24
PHP數組和字符串互相轉換實現方法04-01