開心生活站

位置:首頁 > IT科技 > 

python中endswith

IT科技2.62W

endswith是屬於python下的一個方法,它能夠用來判斷字符串是否以指定後綴結尾,若是以指定後綴結尾返回True,否則是返回False,其中可以選擇參數start與end爲檢索字符串的開始與結束位置。

endswith()方法的語法格式爲:

str.endswith(suffix[, start[, end]])

python中endswith

參數說明:

suffix --該參數能夠是一個字符串或是一個元素。

start -- 字符串中的開始位置。

end -- 字符中結束位置。

返回值:

若是字符串中含有指定的後綴則返回True,否則返回False。

python中endswith 第2張

參考範例:

endswith()方法的使用,具體代碼爲:

#!/usr/bin/python

str = "this is string example....wow!!!";

suffix = "wow!!!";

print str.endswith(suffix);

print str.endswith(suffix,20);

suffix = "is";

print str.endswith(suffix, 2, 4);

print str.endswith(suffix, 2, 6);

輸出結果:

True

True

True

False

標籤:endswith Python