更新时间:2022-09-15 16:52:39
案例:
### 1 ###
str = "01213456" if str.find("23"): print "YES!" else: print "NO!"
结果
1
|
YES! |
### 2 ###
str = "01213456" if str.find("01"): print "YES!" else: print "NO!"
结果
1
|
NO! |
这里注意两点:
1. if str.find('23'): 此时默认为 str.find('23') != 0:
2. find()函数找不到时返回为-1。
经查阅得知其用法:
函数原型:find(str, pos_start, pos_end)
解释:
返回值:如果查到:返回查找的第一个出现的位置。否则,返回-1。
举例论证:
1.str = "0123" print str.find("2",1,-1) #2 2.str = "1111" print str.find("1") #0,首次出现的位置
本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/archive/2012/08/06/2625752.html,如需转载请自行联系原作者