老生常谈Python startswith()函数与endswith函数


Posted in Python onSeptember 08, 2017

函数:startswith()

作用:判断字符串是否以指定字符或子字符串开头

一、函数说明

语法:string.startswith(str, beg=0,end=len(string))
      或string[beg:end].startswith(str)

参数说明:

string:  被检测的字符串
str:      指定的字符或者子字符串。(可以使用元组,会逐一匹配)
beg:    设置字符串检测的起始位置(可选)
end:    设置字符串检测的结束位置(可选)

如果存在参数 beg 和 end,则在指定范围内检查,否则在整个字符串中检查

返回值

如果检测到字符串,则返回True,否则返回False。默认空字符为True

函数解析:如果字符串string是以str开始,则返回True,否则返回False

二、实例

>>> s = 'hello good boy doiido'
>>> print s.startswith('h') 
True
>>> print s.startswith('hel') 
True
>>> print s.startswith('h',4) 
False
>>> print s.startswith('go',6,8) 
True
 
#匹配空字符集 
>>> print s.startswith('') 
True
#匹配元组 
>>> print s.startswith(('t','b','h')) 
True

用环境:用于if判断

>>> if s.startswith('hel'): 
 print "you are right"
else: 
 print "you are wrang"
you are right

函数: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

注:会认为空字符为真

二、实例

>>> 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

常用环境:用于判断文件类型(比如图片,可执行文件)

>>> 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

以上这篇老生常谈Python startswith()函数与endswith函数就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用urllib2防止302跳转的代码例子
Jul 07 Python
利用Python的装饰器解决Bottle框架中用户验证问题
Apr 24 Python
利用Python读取文件的四种不同方法比对
May 18 Python
tensorflow获取变量维度信息
Mar 10 Python
Django学习教程之静态文件的调用详解
May 08 Python
用python简单实现mysql数据同步到ElasticSearch的教程
May 30 Python
pyhanlp安装介绍和简单应用
Feb 22 Python
对Python 检查文件名是否规范的实例详解
Jun 10 Python
利用Python模拟登录pastebin.com的实现方法
Jul 12 Python
使用Python给头像戴上圣诞帽的图像操作过程解析
Sep 20 Python
Python基于smtplib模块发送邮件代码实例
May 29 Python
Python爬虫之Selenium多窗口切换的实现
Dec 04 Python
python学习必备知识汇总
Sep 08 #Python
分享一下如何编写高效且优雅的 Python 代码
Sep 07 #Python
python 函数传参之传值还是传引用的分析
Sep 07 #Python
windows下python之mysqldb模块安装方法
Sep 07 #Python
python 全局变量的import机制介绍
Sep 07 #Python
Python 多线程的实例详解
Sep 07 #Python
Python 闭包的使用方法
Sep 07 #Python
You might like
隐藏X-Space个人空间下方版权方法隐藏X-Space个人空间标题隐藏X-Space个人空间管理版权方法
2007/02/22 PHP
php中mysql操作buffer用法详解
2015/03/19 PHP
php微信开发之谷歌测距
2018/06/14 PHP
JavaScript Event事件学习第一章 Event介绍
2010/02/07 Javascript
JS的反射问题
2010/04/07 Javascript
使用js检测浏览器的实现代码
2013/05/14 Javascript
简单实用的全选反选按钮例子
2013/10/18 Javascript
JS中如何判断传过来的JSON数据中是否存在某字段
2014/08/18 Javascript
jQuery页面元素动态添加后绑定事件丢失方法,非 live
2016/06/16 Javascript
vue 监听某个div垂直滚动条下拉到底部的方法
2018/09/15 Javascript
vue实现购物车功能(商品分类)
2020/04/20 Javascript
vue中jsonp插件的使用方法示例
2020/09/10 Javascript
JavaScript实现页面高亮操作提示和蒙板
2021/01/04 Javascript
利用python获得时间的实例说明
2013/03/25 Python
python用字典统计单词或汉字词个数示例
2014/04/22 Python
给Python的Django框架下搭建的BLOG添加RSS功能的教程
2015/04/08 Python
Python实现在线音乐播放器
2017/03/03 Python
Python中的__slots__示例详解
2017/07/06 Python
Django REST为文件属性输出完整URL的方法
2017/12/18 Python
Opencv-Python图像透视变换cv2.warpPerspective的示例
2019/04/11 Python
Python生成一个迭代器的实操方法
2019/06/18 Python
django 使用全局搜索功能的实例详解
2019/07/18 Python
python实现数学模型(插值、拟合和微分方程)
2020/11/13 Python
使用Python通过oBIX协议访问Niagara数据的示例
2020/12/04 Python
详解CSS3原生支持div铺满浏览器的方法
2018/08/30 HTML / CSS
用html5的canvas画布绘制贝塞尔曲线完整代码
2013/08/14 HTML / CSS
amazeui页面分析之登录页面的示例代码
2020/08/25 HTML / CSS
世界上最好的野生海鲜和有机食品:Vital Choice
2020/01/16 全球购物
简述你对Statement,PreparedStatement,CallableStatement的理解
2013/03/25 面试题
毕业自我评价
2014/02/05 职场文书
2014年医学生毕业自我鉴定
2014/03/26 职场文书
詹天佑教学反思
2014/04/30 职场文书
庆六一活动总结
2014/08/29 职场文书
企业安全生产检查制度
2015/08/06 职场文书
初中班主任教育随笔
2015/08/15 职场文书
Nginx开源可视化配置工具NginxConfig使用教程
2022/06/21 Servers