Python3 中的 find(substring ,beginIn

首页 / Python3入门教程 / Python3 中的 find(substring ,beginIn

Python find()方法在整个字符串中找到子字符串,并返回第一个匹配项的索引。如果子字符串不匹配,则返回-1。

find - 语法

find(sub[, start[, end]])

find - 参数

  • sub    - 子字符串
  • start  - 开始索引一个范围
  • end    - 范围的最后一个索引

find - 返回类型

如果找到,则返回子字符串的索引,否则为-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

无涯教程还要指定其他参数,并使搜索更加自定义。

# Python find() function example
# 变量声明
str = "Welcome to the Learnfk."
# 函数调用
str2 = str.find("t")
str3 = str.find("t",25)
# 显示结果
print(str2)
print(str3)

输出

8
-1
find - Python字符串find()方法示例4
# 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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

OpenResty从入门到实战 -〔温铭〕

苏杰的产品创新课 -〔苏杰〕

检索技术核心20讲 -〔陈东〕

Redis核心技术与实战 -〔蒋德钧〕

WebAssembly入门课 -〔于航〕

分布式金融架构课 -〔任杰〕

攻克视频技术 -〔李江〕

网络排查案例课 -〔杨胜辉〕

结构写作力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)