python find()用法

 

案例:

### 1 ###

str = "01213456"

if str.find("23"):
    print "YES!"
else:
    print  "NO!"

### 2 ###

str = "01213456"

if str.find("23"):
    print "YES!"
else:
    print  "NO!"

上两个案例结果都为“YES!”, 非常令我吃惊,2不应该是NO!吗?经查阅得知其用法:

函数原型:find(str, pos_start, pos_end)

解释:

  • str:被查找“字串”
  • pos_start:查找的首字母位置(从0开始计数。默认:0
  • pos_end: 查找的末尾位置(默认-1

返回值:如果查到:返回查找的第一个出现的位置。否则,返回-1

举例论证:

           1.str = "0123"
                     print str.find("2",1,-1)      #2

           2.str = "1111"
                     print str.find("1")           #0,首次出现的位置

 

 
 
 
 
posted @ 2013-04-11 23:56  wuxi812  阅读(249)  评论(0编辑  收藏  举报