Python find()方法在整个字符串中找到子字符串,并返回第一个匹配项的索引。如果子字符串不匹配,则返回-1。
find(sub[, start[, end]])
如果找到,则返回子字符串的索引,否则为-1。
简单查找方法的一个示例,该方法仅采用单个参数(子字符串)。
# Python find() function example # 变量声明 str = "Welcome to the Learnfk." # 函数调用 str2 = str.find("the") # 显示结果 print(str2)
输出:
11
如果找不到任何匹配项,则返回-1,请参见示例。
# Python find() function example # 变量声明 str = "Welcome to the Learnfk." # 函数调用 str2 = str.find("is") # 显示结果 print(str2)
输出:
-1
无涯教程还要指定其他参数,并使搜索更加自定义。
链接:https://www.learnfk.comhttps://www.learnfk.com/python3/python-string-find-method.html
来源:LearnFk无涯教程网
# Python find() function example # 变量声明 str = "Welcome to the Learnfk." # 函数调用 str2 = str.find("t") str3 = str.find("t",25) # 显示结果 print(str2) print(str3)
输出:
8 -1
# Python find() function example # 变量声明 str = "Welcome to the Learnfk." # 函数调用 str2 = str.find("t") str3 = str.find("t",20,25) # 显示结果 print(str2) print(str3)
输出:
8 24
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)