Python的startswith与endswith函数
来源:互联网 发布:arm模拟器 windows 编辑:程序博客网 时间:2024/04/30 00:42
http://www.qttc.net/201209206.html
在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 '这不是一张图片'
# 执行结果:这是一张图片
0 0
- Python的startswith与endswith函数
- Python的startswith与endswith函数
- python startswith与endswith
- Python的startswith和endswith
- Python的startswith和endswith
- Python的startswith和endswith
- Python的 startswith 和endswith
- 不曾见过的Python函数——startswith(),endswith()
- python中startswith与endswith方法
- 【Python】Python startswith, endswith
- python中startswith和endswith的使用
- Python中startswith和endswith的用法
- python startswith and endswith
- python startswith和endswith
- Python startswith endswith
- startswith和endswith函数
- java中startsWith与endsWith的用法
- .startsWith和endsWith的使用方法与说明
- css:border-sizing
- GoF中的23种模式
- 【Android】多个EditText设置其中一个不可编辑并隐藏键盘
- git id_rsa are too open的解决办法
- 刷新UI方法
- Python的startswith与endswith函数
- 表的修改
- js正确截取单字节和双字节混和字符串的方法
- 校园网网络连接反复断开又连接是什么原因?
- Spring MVC集成Tiles使用方法
- weblogic11g 启动时提示输入密码的问题
- Demo
- Java中使用Runtime和Process类运行外部程序
- Android 通过浏览器打开App