国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中的startswith和endswith函數使用實例

2020-02-23 05:42:01
字體:
來源:轉載
供稿:網友

在Python中有兩個函數分別是startswith()函數與endswith()函數,功能都十分相似,startswith()函數判斷文本是否以某個字符開始,endswith()函數判斷文本是否以某個字符結束。

startswith()函數

此函數判斷一個文本是否以某個或幾個字符開始,結果以True或者False返回。
代碼如下:
text='welcome to qttc blog'
print text.startswith('w')      # True
print text.startswith('wel')    # True
print text.startswith('c')      # False
print text.startswith('')       # True

endswith()函數

此函數判斷一個文本是否以某個或幾個字符結束,結果以True或者False返回。
代碼如下:
text='welcome to qttc blog'
print text.endswith('g')        # True
print text.endswith('go')       # False
print text.endswith('og')       # True
print text.endswith('')         # True
print text.endswith('g ')       # False

判斷文件是否為exe執行文件

我們可以利用endswith()函數判斷文件名的是不是以.exe后綴結尾判斷是否為可執行文件
代碼如下:
# coding=utf8
 
fileName1='qttc.exe'
if(fileName1.endswith('.exe')):
    print '這是一個exe執行文件'  
else:
    print '這不是一個exe執行文件'
 
# 執行結果:這是一個exe執行文件

判斷文件名后綴是否為圖片

代碼如下:
# coding=utf8
 
fileName1='pic.jpg'
if fileName1.endswith('.gif') or fileName1.endswith('.jpg') or fileName1.endswith('.png'):
    print '這是一張圖片'
else:
    print '這不是一張圖片'
    
# 執行結果:這是一張圖片

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿勒泰市| 崇礼县| 安康市| 凤阳县| 余庆县| 潮安县| 大余县| 鹤庆县| 双桥区| 都江堰市| 临泽县| 东兰县| 金昌市| 丹棱县| 平邑县| 沙雅县| 沧源| 新疆| 沁水县| 张掖市| 吉首市| 廊坊市| 武威市| 花莲市| 始兴县| 莫力| 灵丘县| 东台市| 新龙县| 曲沃县| 北安市| 遂川县| 抚顺县| 塔城市| 大足县| 霍林郭勒市| 黄梅县| 贵南县| 常州市| 浦县| 琼海市|