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

返回前面的内容

相关阅读 >>

Python中关于变量赋值操作的实例分享

字符串格式化 % vs format哪种更好

Python配置mysql的教程(必看)

Python使用opencv进行标定

Python topn 取最大的n个数或最小的n个数方法

Python终端会话是什么

Python中for是什么

Python怎么求加到n项和

hash()是Python内置的吗

Python文件扩展名是什么

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




打赏

取消

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

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

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

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

评论

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