用python語言,如何判斷一段字符串中是否包含指定的字符串比如abcdefghi中是否包含bcd,是返回真否則返回假,怎樣寫代碼
2 回答

ITMISS
TA貢獻1871條經驗 獲得超8個贊
python的string對象沒有contains方法,不用使用string.contains的方法判斷是否包含子字符串,但是python有更簡單的方法來替換contains函數。
方法1:使用 in 方法實現contains的功能:
site = ''
if "jb51" in site:
print('site contains jb51')
輸出結果:site contains jb51
方法2:使用find函數實現contains的功能
s = "This be a string"
if s.find("is") == -1:
print "No 'is' here!"
else:
print "Found 'is' in the string."
添加回答
舉報
0/150
提交
取消