Python startswith()和endswith() 方法原理解析


Posted in Python onApril 28, 2020

startswith()方法

Python startswith() 方法用于检查字符串是否是以指定子字符串开头

如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。

str.startswith(str, beg=0,end=len(string));

参数

  • str --检测的字符串。
  • strbeg --可选参数用于设置字符串检测的起始位置。
  • strend --可选参数用于设置字符串检测的结束位置。

返回值

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

常用环境:用于IF判断

#!/usr/local/bin/python
# coding=utf-8
listsql = 'select * from ifrs.indiv_info'
def isSelect(sql):
  chsql = sql.upper().strip()
  if not chsql.startswith("SELECT "):
    return False
  return True

print isSelect(listsql)
[root@bigdata-poc-shtz-3 zw]# python h.py
True

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

注:会认为空字符为真

python
>>> endsql = 'select * from ifrs.indiv_info'
>>> endsql.endswith('info')
True
>>> endsql.endswith('info',3)
True
>>>
>>> endsql.endswith('info',3,10)
False
>>> endsql.endswith('info',25,29)
True
>>> endsql.endswith('')
True

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

>>> f = 'a.txt'
>>> if f.endswith(('.txt')):
... print '%s is a txt' %f
... else:
... print '%s is not a txt' %f
...
a.txt is a txt

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python+Django在windows下的开发环境配置图解
Nov 11 Python
Python将阿拉伯数字转换为罗马数字的方法
Jul 10 Python
在PyCharm环境中使用Jupyter Notebook的两种方法总结
May 24 Python
python实现requests发送/上传多个文件的示例
Jun 04 Python
Python求两个圆的交点坐标或三个圆的交点坐标方法
Nov 07 Python
Python二叉搜索树与双向链表转换算法示例
Mar 02 Python
pycharm new project变成灰色的解决方法
Jun 27 Python
用Python生成HTML表格的方法示例
Mar 06 Python
python 带时区的日期格式化操作
Oct 23 Python
详解pandas中利用DataFrame对象的.loc[]、.iloc[]方法抽取数据
Dec 13 Python
Python绘制分类图的方法
Apr 20 Python
Python-typing: 类型标注与支持 Any类型详解
May 10 Python
Python如何将函数值赋给变量
Apr 28 #Python
Python多线程thread及模块使用实例
Apr 28 #Python
Python基于模块Paramiko实现SSHv2协议
Apr 28 #Python
Python内置函数locals和globals对比
Apr 28 #Python
使用python实现CGI环境搭建过程解析
Apr 28 #Python
基于python连接oracle导并出数据文件
Apr 28 #Python
numpy库ndarray多维数组的维度变换方法(reshape、resize、swapaxes、flatten)
Apr 28 #Python
You might like
Cappuccino 卡布其诺咖啡之制作
2021/03/03 冲泡冲煮
thinkPHP中多维数组的遍历方法
2016/01/09 PHP
详解YII关联查询
2016/01/10 PHP
javascript实现的在当前窗口中漂浮框的代码
2010/03/15 Javascript
jQuery初学:find()方法及children方法的区别分析
2011/01/31 Javascript
jQuery实现页面滚动时层智能浮动定位实例探讨
2013/03/29 Javascript
javascript抽象工厂模式详细说明
2014/12/16 Javascript
javascript修改图片src的方法
2015/01/27 Javascript
jQuery实现简洁的导航菜单效果
2015/11/23 Javascript
js中遍历对象的属性和值的方法
2016/07/27 Javascript
使用nodejs中httpProxy代理时候出现404异常的解决方法
2016/08/15 NodeJs
浅析Javascript ES6新增值比较函数Object.is
2016/08/24 Javascript
AngularJs bootstrap搭载前台框架——js控制部分
2016/09/01 Javascript
浅谈js script标签中的预解析
2016/12/30 Javascript
canvas 弹幕效果(实例分享)
2017/01/11 Javascript
JavaScript判断浏览器及其版本信息
2017/01/20 Javascript
详解angular ui-grid之过滤器设置
2017/06/07 Javascript
深入理解vue.js中的v-if和v-show
2017/06/22 Javascript
AngularJs用户登录问题处理(交互及验证、阻止FQ处理)
2017/10/26 Javascript
使用Angular CLI从蓝本生成代码详解
2018/03/24 Javascript
vue使用Google地图的实现示例代码
2018/12/19 Javascript
vue实现多组关键词对应高亮显示功能
2019/07/25 Javascript
[42:20]2014 DOTA2华西杯精英邀请赛5 24 DK VS NewBee
2014/05/25 DOTA
Python实现数通设备端口使用情况监控实例
2015/07/15 Python
Python3.7 新特性之dataclass装饰器
2019/05/27 Python
Numpy 理解ndarray对象的示例代码
2020/04/03 Python
基于HTML5 audio元素播放声音jQuery小插件
2011/05/11 HTML / CSS
英国最大的电脑零售连锁店集团:PC World
2016/10/10 全球购物
Nicole Miller官方网站:纽约女装品牌
2019/09/14 全球购物
英国现代、当代和设计师家具店:Furntastic
2020/07/18 全球购物
岗位聘任协议书
2015/09/21 职场文书
预备党员表决心的话
2015/09/22 职场文书
读《教育心理学》心得体会
2016/01/22 职场文书
基于Redis位图实现用户签到功能
2021/05/08 Redis
Pytorch可视化的几种实现方法
2021/06/10 Python
Js类的构建与继承案例详解
2021/09/15 Javascript