好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

下面小编就为大家带来一篇老生常谈Python startswith()函数与endswith函数。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

>>> s = 'hello good boy doiido'
>>> print s.startswith('h') 
True
>>> print s.startswith('hel') 
True
>>> print s.startswith('h',4) 
False
>>> print s.startswith('go',6,8) 
True
 
#匹配空字符集 
>>> print s.startswith('') 
True
#匹配元组 
>>> print s.startswith(('t','b','h')) 
True 
>>> if s.startswith('hel'): 
 print "you are right"
else: 
 print "you are wrang"
you are right 
>>> 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 
>>> 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函数的使用方法的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于详解Python中startswith()函数与endswith函数的使用方法的详细内容...

  阅读:36次