>>> 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函数的使用方法的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did84470