Python中endswith()函數(shù)的基本使用
函數(shù):endswith()
作用:判斷字符串是否以指定字符或子字符串結(jié)尾,常用于判斷文件類型
相關(guān)函數(shù):判斷字符串開(kāi)頭 startswith()
一、函數(shù)說(shuō)明
語(yǔ)法:string.endswith(str, beg=[0,end=len(string)])
string[beg:end].endswith(str)
參數(shù)說(shuō)明:
string: 被檢測(cè)的字符串
str: 指定的字符或者子字符串(可以使用元組,會(huì)逐一匹配)
beg: 設(shè)置字符串檢測(cè)的起始位置(可選,從左數(shù)起)
end: 設(shè)置字符串檢測(cè)的結(jié)束位置(可選,從左數(shù)起)
如果存在參數(shù) beg 和 end,則在指定范圍內(nèi)檢查,否則在整個(gè)字符串中檢查
返回值:
如果檢測(cè)到字符串,則返回True,否則返回False。
解析:如果字符串string是以str結(jié)束,則返回True,否則返回False
注:會(huì)認(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
相關(guān)文章
關(guān)于Python調(diào)用百度語(yǔ)音合成SDK實(shí)現(xiàn)文字轉(zhuǎn)音頻的方法
這篇文章主要介紹了關(guān)于Python調(diào)用百度語(yǔ)音合成SDK實(shí)現(xiàn)文字轉(zhuǎn)音頻的方法,AipSpeech是語(yǔ)音合成的Python?SDK客戶端,為使用語(yǔ)音合成的開(kāi)發(fā)人員提供了一系列的交互方法,需要的朋友可以參考下2023-07-07Python入門學(xué)習(xí)之字符串與比較運(yùn)算符
這篇文章主要介紹了Python入門學(xué)習(xí)之字符串與比較運(yùn)算符,是Python語(yǔ)法中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10解決python中顯示圖片的plt.imshow plt.show()內(nèi)存泄漏問(wèn)題
這篇文章主要介紹了解決python中顯示圖片的plt.imshow plt.show()內(nèi)存泄漏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04