七彩读写,闪讯无线,于大宝女友张晓婷
startswith()方法
python startswith() 方法用于检查字符串是否是以指定子字符串开头
如果是则返回 true,否则返回 false。如果参数 beg 和 end 指定值,则在指定范围内检查。
str.startswith(str, beg=0,end=len(string));
参数
返回值
如果检测到字符串则返回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)
参数说明:
如果存在参数 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爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论