详解Python中startswith()函数与endswith函数的使用方法


当前第2页 返回上一页

解析:如果字符串string是以str结束,则返回True,否则返回False

注:会认为空字符为真

二、实例


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

>>> 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

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


1

2

3

4

5

6

>>> 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函数的使用方法的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

win7系统怎么安装Python

一文通读Python自定义函数与Python函数返回值,附有详细示例

Python中的map怎么使用(方法详解)

Python中int是什么意思

Python的序列之列表的通用方法

Python基于flask上传文件的代码示例

Python中pylint使用方法(pylint代码检查)_Python

Python的int是什么

Python只能做爬虫么

比较import reload __import__在Python中的用法区别

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...