python字符串-查找(find)
find方法是一个基本的字符串查找的操作。
- find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
语法:
S.find(sub[, start[, end]]) -> int
参数
- str:要查找的字符串。
- start:可选参数,开始索引,默认为0。
- end:可选参数,结束索引,默认为字符串长度。
返回值
- 如果包含子字符串返回开始的索引值,否则返回-1。
示例
str = '我爱我的爸妈'
print('返回子字符串开始的偏移量:', str.find('我的'))
print('指定开始索引:', str.find('我的', 3))
print('指定结束索引:', str.find('我的', 0, 2))
print('没有找到的情况下返回-1:', str.find('姐姐'))
返回子字符串开始的偏移量: 2
指定开始索引: -1
指定结束索引: -1
没有找到的情况下返回-1: -1
help(str.find)
find(...) method of builtins.str instance
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!