老生常谈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 相关文章推荐
windows系统中python使用rar命令压缩多个文件夹示例
May 06 Python
Python生成随机MAC地址
Mar 10 Python
Python3中的2to3转换工具使用示例
Jun 12 Python
Python for Informatics 第11章 正则表达式(一)
Apr 21 Python
基于python时间处理方法(详解)
Aug 14 Python
python表格存取的方法
Mar 07 Python
PyQt实现界面翻转切换效果
Apr 20 Python
python实现音乐下载的统计
Jun 20 Python
python tkinter实现界面切换的示例代码
Jun 14 Python
opencv-python 提取sift特征并匹配的实例
Dec 09 Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧(自定义信号与槽)详解与实例
Mar 06 Python
Python3 用matplotlib绘制sigmoid函数的案例
Dec 11 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
PHP 长文章分页函数 带使用方法,不会分割段落,翻页在底部
2009/10/22 PHP
php实现的简易扫雷游戏实例
2015/07/09 PHP
PHP实现的简单缓存类
2015/07/29 PHP
PHP如何将XML转成数组
2016/04/04 PHP
JavaScript 事件查询综合
2009/07/13 Javascript
jQuery TextBox自动完成条
2009/07/22 Javascript
JQuery 选择器、过滤器介绍
2011/02/14 Javascript
JS定时关闭窗口的实例
2013/05/22 Javascript
jquery选择器之内容过滤选择器详解
2014/01/27 Javascript
js实现简单的联动菜单效果
2015/08/19 Javascript
jquery中checkbox使用方法简单实例演示
2015/11/24 Javascript
require.js+vue开发微信上传图片组件
2016/10/27 Javascript
jQuery 1.9版本以上的浏览器判断方法代码分享
2017/08/28 jQuery
在vue里面设置全局变量或数据的方法
2018/03/09 Javascript
JS实现模糊查询带下拉匹配效果
2018/06/21 Javascript
video.js 一个页面同时播放多个视频的实例代码
2018/11/27 Javascript
Node.js 多线程完全指南总结
2019/03/27 Javascript
微信小程序学习笔记之函数定义、页面渲染图文详解
2019/03/28 Javascript
python中pycurl库的用法实例
2014/09/30 Python
深入理解 Python 中的多线程 新手必看
2016/11/20 Python
对pandas通过索引提取dataframe的行方法详解
2019/02/01 Python
Python代码实现删除一个list里面重复元素的方法
2019/04/02 Python
python读写csv文件的方法
2019/08/13 Python
Python3直接爬取图片URL并保存示例
2019/12/18 Python
TensorFlow tf.nn.conv2d实现卷积的方式
2020/01/03 Python
tensorflow:指定gpu 限制使用量百分比,设置最小使用量的实现
2020/02/06 Python
Python暴力破解Mysql数据的示例
2020/11/09 Python
英国奢侈品在线精品店:Hervia
2020/09/03 全球购物
求职简历的自我评价
2014/01/31 职场文书
大学生作弊检讨书
2014/02/19 职场文书
运动会跳远加油稿
2014/02/20 职场文书
2014年团队工作总结
2014/11/24 职场文书
党员考试作弊检讨书1000字
2015/02/16 职场文书
大专护理专业自荐信
2015/03/25 职场文书
护士长2015年终工作总结
2015/04/24 职场文书
医者仁心观后感
2015/06/17 职场文书