老生常谈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设置tmpfs来加速项目的教程
Apr 17 Python
Python中输出ASCII大文字、艺术字、字符字小技巧
Apr 28 Python
Python找出list中最常出现元素的方法
Jun 14 Python
简单实现python数独游戏
Mar 30 Python
Python3.6基于正则实现的计算器示例【无优化简单注释版】
Jun 14 Python
浅谈Python接口对json串的处理方法
Dec 19 Python
利用Python实现微信找房机器人实例教程
Mar 10 Python
使用TensorFlow对图像进行随机旋转的实现示例
Jan 20 Python
tensorflow多维张量计算实例
Feb 11 Python
python 截取XML中bndbox的坐标中的图像,另存为jpg的实例
Mar 10 Python
python matplotlib实现将图例放在图外
Apr 17 Python
Python使用pandas导入csv文件内容的示例代码
Dec 24 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之字符串变相相减的代码
2007/03/19 PHP
php addslashes 函数详细分析说明
2009/06/23 PHP
基于php使用memcache存储session的详解
2013/06/25 PHP
Ajax::prototype 源码解读
2007/01/22 Javascript
js字母大小写转换实现方法总结
2013/11/13 Javascript
无刷新预览所选择的图片示例代码
2014/04/02 Javascript
angularJS 中$attrs方法使用指南
2015/02/09 Javascript
jQuery+slidereveal实现的面板滑动侧边展出效果
2015/03/14 Javascript
JQuery radio(单选按钮)操作方法汇总
2015/04/15 Javascript
jquery实现仿新浪微博带动画效果弹出层代码(可关闭、可拖动)
2015/10/12 Javascript
Bootstrap入门书籍之(一)排版
2016/02/17 Javascript
纯JS代码实现一键分享功能
2016/04/20 Javascript
关于Iframe父页面与子页面之间的相互调用
2016/11/22 Javascript
JavaScript中利用for循环遍历数组
2017/01/15 Javascript
JS实现侧边栏鼠标经过弹出框+缓冲效果
2017/03/29 Javascript
JavaScript实现跟随滚动缓冲运动广告框
2017/07/15 Javascript
JavaScript中Hoisting详解 (变量提升与函数声明提升)
2017/08/18 Javascript
js循环map 获取所有的key和value的实现代码(json)
2018/05/09 Javascript
微信小程序实现动态显示和隐藏某个控件功能示例
2018/12/14 Javascript
element-ui表格合并span-method的实现方法
2019/05/21 Javascript
[34:08]2018DOTA2亚洲邀请赛3月29日 小组赛B组 VP VS EG
2018/03/30 DOTA
matplotlib在python上绘制3D散点图实例详解
2017/12/09 Python
tf.truncated_normal与tf.random_normal的详细用法
2018/03/05 Python
利用python将图片版PDF转文字版PDF
2019/05/03 Python
解决pyinstaller打包发布后的exe文件打开控制台闪退的问题
2019/06/21 Python
python的sorted用法详解
2019/06/25 Python
Django 接收Post请求数据,并保存到数据库的实现方法
2019/07/12 Python
Python sorted排序方法如何实现
2020/03/31 Python
python小白切忌乱用表达式
2020/05/29 Python
keras 简单 lstm实例(基于one-hot编码)
2020/07/02 Python
HTML5 Canvas的常用线条属性值总结
2016/03/17 HTML / CSS
台湾饭店和机票预订网站:Expedia台湾
2016/08/05 全球购物
彪马美国官网:PUMA美国
2017/03/09 全球购物
英国网上购买门:Direct Doors
2018/06/07 全球购物
幼儿园欢迎词范文
2015/01/26 职场文书
Python OpenCV超详细讲解基本功能
2022/04/02 Python