pythonfind函数的功能和用法
时间:2026-05-21 16:54:35来源:`find()` 是 Python 中字符串方法之一,用于查找子字符串在原字符串中的位置。若未找到,则返回 -1。
| 功能 | 用法 | 示例 | 返回值 |
| 查找子字符串 | `str.find(sub)` | `"hello".find("e")` | 1 |
| 查找子字符串(起始位置) | `str.find(sub, start)` | `"hello".find("l", 2)` | 3 |
| 查找子字符串(结束位置) | `str.find(sub, start, end)` | `"hello".find("o", 0, 4)` | -1 |
`find()` 方法不会修改原字符串,仅返回索引。相比 `index()` 方法,`find()` 在未找到时返回 -1,更安全。适用于需要判断子字符串是否存在的情况。
展开更多
标签:
