當前位置:科普知識站>IT科技>

python中endswith

IT科技 閱讀(2.65W)

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