函數(shù):endswith()
作用:判斷字符串是否以指定字符或子字符串結(jié)尾,常用于判斷文件類型
相關(guān)函數(shù):判斷字符串開頭 startswith()
一、函數(shù)說明
語法:string.endswith(str, beg=[0,end=len(string)])
?????????? string[beg:end].endswith(str)
參數(shù)說明:
string: 被檢測的字符串
str:????? 指定的字符或者子字符串(可以使用元組,會逐一匹配)
beg:??? 設(shè)置字符串檢測的起始位置(可選,從左數(shù)起)
end:??? 設(shè)置字符串檢測的結(jié)束位置(可選,從左數(shù)起)
如果存在參數(shù) beg 和 end,則在指定范圍內(nèi)檢查,否則在整個(gè)字符串中檢查?
?
返回值:
如果檢測到字符串,則返回True,否則返回False。
解析:如果字符串string是以str結(jié)束,則返回True,否則返回False
注:會認(rèn)為空字符為真
二、實(shí)例
?
>>> s = 'hello good boy doiido' >>> print s.endswith('o') True >>> print s.endswith('ido') True >>> print s.endswith('do',4) True >>> print s.endswith('do',4,15) False
#匹配空字符集 >>> print s.endswith('') True #匹配元組 >>> print s.endswith(('t','b','o')) True
?
常用環(huán)境:用于判斷文件類型(比如圖片,可執(zhí)行文件)
>>> f = 'pic.jpg' >>> if f.endswith(('.gif','.jpg','.png')): print '%s is a pic' %f else: print '%s is not a pic' %f pic.jpg is a pic
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
