老生常谈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统计文本字符串里单词出现频率的方法
May 26 Python
python条件变量之生产者与消费者操作实例分析
Mar 22 Python
python中实现延时回调普通函数示例代码
Sep 08 Python
Python3实现的字典遍历操作详解
Apr 18 Python
解决tensorflow测试模型时NotFoundError错误的问题
Jul 27 Python
python实现可变变量名方法详解
Jul 01 Python
python3实现带多张图片、附件的邮件发送
Aug 10 Python
详解Python time库的使用
Oct 10 Python
Python List列表对象内置方法实例详解
Oct 22 Python
python GUI库图形界面开发之pyinstaller打包python程序为exe安装文件
Feb 26 Python
用python实现名片管理系统
Jun 18 Python
基于Python实现流星雨效果的绘制
Mar 18 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下通过POST还是GET来传值
2008/06/05 PHP
php运行时动态创建函数的方法
2015/03/16 PHP
Symfony2学习笔记之系统路由详解
2016/03/17 PHP
thinkPHP5框架自定义验证器实现方法分析
2018/06/11 PHP
Javascript学习笔记二 之 变量
2010/12/15 Javascript
JavaScript中OnLoad几种使用方法
2012/12/15 Javascript
了不起的node.js读书笔记之mongodb数据库交互
2014/12/22 Javascript
JSON字符串和对象之间的转换详解
2015/05/26 Javascript
使用coffeescript编写node.js项目的方法汇总
2015/08/05 Javascript
快速解决Canvas.toDataURL 图片跨域的问题
2016/05/10 Javascript
JQuery实现文字无缝滚动效果示例代码(Marquee插件)
2017/03/07 Javascript
jQuery查找dom的几种方法效率详解
2017/05/17 jQuery
在Web关闭页面时发送Ajax请求的实现方法
2019/03/07 Javascript
分享Angular http interceptors 拦截器使用(推荐)
2019/11/10 Javascript
决策树的python实现方法
2014/11/18 Python
Windows下Python使用Pandas模块操作Excel文件的教程
2016/05/31 Python
Python输出汉字字库及将文字转换为图片的方法
2016/06/04 Python
使用python实现ANN
2017/12/20 Python
Django使用Celery异步任务队列的使用
2018/03/13 Python
python绘制热力图heatmap
2020/03/23 Python
Python3删除排序数组中重复项的方法分析
2019/01/31 Python
Python使用统计函数绘制简单图形实例代码
2019/05/15 Python
简单了解python单例模式的几种写法
2019/07/01 Python
pandas DataFrame创建方法的方式
2019/08/02 Python
python 实现快速生成连续、随机字母列表
2019/11/28 Python
pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率
2020/01/02 Python
Python调用shell命令常用方法(4种)
2020/05/11 Python
Alpine安装Python3依赖出现的问题及解决方法
2020/12/25 Python
css3实现蒙版弹幕功能
2019/06/18 HTML / CSS
HTML5页面无缝闪开的问题及解决方案
2020/06/11 HTML / CSS
招股说明书范本
2014/05/06 职场文书
化工工艺设计求职信
2014/06/25 职场文书
个人公司授权委托书范本
2014/10/12 职场文书
购房委托书
2014/10/15 职场文书
商家认证委托书格式
2014/10/16 职场文书
Java实现简单小画板
2022/06/10 Java/Android