1. 程式人生 > >Python endswith() 函式 判斷字串結尾

Python endswith() 函式 判斷字串結尾

一、函式說明
語法:string.endswith(str, beg=[0,end=len(string)])
           string[beg:end].endswith(str)

引數說明:
string: 被檢測的字串
str:      指定的字元或者子字串(可以使用元組,會逐一匹配)
beg:    設定字串檢測的起始位置(可選,從左數起)
end:    設定字串檢測的結束位置(可選,從左數起)
如果存在引數 beg 和 end,則在指定範圍內檢查,否則在整個字串中檢查 
 
返回值:
如果檢測到字串,則返回True,否則返回False。

解析:如果字串string是以str結束,則返回True,否則返回False

注:會認為空字元為真


二、例項