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中关于日期时间处理的问答集锦
Mar 08 Python
python处理中文编码和判断编码示例
Feb 26 Python
Python简单日志处理类分享
Feb 14 Python
python 性能提升的几种方法
Jul 15 Python
python机器学习理论与实战(五)支持向量机
Jan 19 Python
python中的二维列表实例详解
Jun 19 Python
python文本数据处理学习笔记详解
Jun 17 Python
Python3批量移动指定文件到指定文件夹方法示例
Sep 02 Python
使用pyecharts1.7进行简单的可视化大全
May 17 Python
如何在Python中创建二叉树
Mar 30 Python
python四种出行路线规划的实现
Jun 23 Python
Python 装饰器(decorator)常用的创建方式及解析
Apr 24 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
javascript 小型动画组件与实现代码
2010/06/02 PHP
采集邮箱的php代码(抓取网页中的邮箱地址)
2012/07/17 PHP
php 把数字转换成汉字的代码
2015/07/21 PHP
PHP实现的网站目录扫描索引工具
2016/09/08 PHP
PHP7 参数处理机制修改
2021/03/09 PHP
prototype 中文参数乱码解决方案
2009/11/09 Javascript
javascript中bind函数的作用实例介绍
2014/09/28 Javascript
js解决select下拉选不中问题
2014/10/14 Javascript
JavaScript中Cookies的相关使用教程
2015/06/04 Javascript
Jquery实现纵向横向菜单
2016/01/24 Javascript
JavaScript操作class和style样式代码详解
2016/02/13 Javascript
RequireJS使用注意细节
2016/05/15 Javascript
手机浏览器 后退按钮强制刷新页面方法总结
2016/10/09 Javascript
JavaScript实现定时页面跳转功能示例
2017/02/14 Javascript
OkHttp踩坑随笔为何 response.body().string() 只能调用一次
2018/01/08 Javascript
vue中简单弹框dialog的实现方法
2018/02/26 Javascript
详解Angular如何正确的操作DOM
2018/07/06 Javascript
微信小程序自定义带价格显示日历效果
2018/12/29 Javascript
小程序获取当前位置加搜索附近热门小区及商区的方法
2019/04/08 Javascript
分享Angular http interceptors 拦截器使用(推荐)
2019/11/10 Javascript
jquery实现进度条状态展示
2020/03/26 jQuery
微信小程序实现加入购物车滑动轨迹
2020/11/18 Javascript
[01:12](回顾)DOTA2国际邀请赛,全世界DOTAer的盛宴
2014/07/01 DOTA
Python中的面向对象编程详解(上)
2015/04/13 Python
利用Python实现简单的相似图片搜索的教程
2015/04/23 Python
Python使用PIL库实现验证码图片的方法
2016/03/11 Python
python中使用print输出中文的方法
2018/07/16 Python
python实现两个dict合并与计算操作示例
2019/07/01 Python
关于TensorFlow新旧版本函数接口变化详解
2020/02/10 Python
Python Json数据文件操作原理解析
2020/05/09 Python
基于python tkinter的点名小程序功能的实例代码
2020/08/22 Python
HTML5 创建canvas元素示例代码
2014/06/04 HTML / CSS
室内设计专业个人的自我评价
2013/10/19 职场文书
学生自我鉴定格式及范文
2014/09/16 职场文书
2014年监理个人工作总结
2014/12/11 职场文书
苹果M1芯片安装nginx 并且部署vue项目步骤详解
2021/11/20 Servers