python find命令、startwith命令

python的字符串有很多好用的操作,比如find,startswith命令。

这几个命令在处理配置文件的时候很有用,比如用startswith判断是否是注释行。

注意:几个函数的返回值是不同滴。

函数原型:find(str, pos_start, pos_end),返回-1或第一个查找到的位置。

              startswith(str),返回false和true

              endswith(str),返回false和true

find命令:

>>> str1 = 'hello, world'
>>> print str1.find('he')
0
>>> print str1.find('wo ')
-1

startswith命令和endswith命令:

>>> str1 = 'hello, world'
>>> print str1.startswith('he')
True
>>> print str1.endswith('ld')
True

 

posted on 2015-08-02 20:50  chybot  阅读(529)  评论(0编辑  收藏  举报