API文档:
string.find(s,sub[,start[,end]])
-
Return the lowest index in s where the substringsub is found such thatsub is wholly contained ins[start:end]. Return-1 on failure. Defaults forstart andend and interpretation of negative values is the same as for slices.
文档理解:
s:被匹配的字符串
sub:匹配的字符串
start:字符串的开始位置
end:字符串结束为止
对子字符串sub在s字符串中进行搜索。如果没有匹配到则返回-1,查找到字符串返回索引最低的索引值
例子:
#! /usr/bin/env python
#coding=utf-8
import string
line1="and < abc >"
line2="a b > c"
print string.find(line1,'nd',0,3)
print string.find(line2,'a b')
print string.find(line2,'d')
输出:
1
0
-1