`find` 是 Python 中的一个内置函数,用于在字符串中查找子字符串的索引。如果找到子字符串,`find` 函数返回子字符串在原始字符串中第一次出现的索引位置;如果没有找到,则返回 `-1`。
`find` 函数的语法如下:
```python
str.find(sub[, start[, end]])
`str`:要在其中查找子字符串的原始字符串。
`sub`:要在 `str` 中查找的子字符串。
`start`(可选):查找的起始索引,默认为 `0`。
`end`(可选):查找的结束索引,默认为字符串的长度。
下面是一些使用 `find` 函数的示例:
```python
text = "Hello, world!"
查找子字符串 "world"
index = text.find("world")
print(index) 输出:7
查找子字符串 "planet",从索引 7 开始
index = text.find("planet", 7)
print(index) 输出:-1,因为 "planet" 不在索引 7 之后出现
查找子字符串 "o",从索引 5 到字符串末尾
index = text.find("o", 5, len(text))
print(index) 输出:4