find() 是 Python 中字符串(string)类型的一个方法,它用于在一个字符串中查找指定的子字符串。如果找到了子字符串,则返回子字符串在原字符串中首次出现的位置(索引),否则返回 -1。
str.find(sub[, start[, end]])参数说明:
sub:必需,要查找的子字符串。start:可选,从哪个位置开始查找。默认为 0。end:可选,在哪个位置结束查找。默认为字符串长度。示例:text = "Hello, welcome to my world."# 查找子字符串 "welcome"result = text.find("welcome")print(result) # 输出:7# 从位置 10 开始查找子字符串 "o"result = text.find("o", 10)print(result) # 输出:15# 在位置 10 到 20 之间查找子字符串 "o"result = text.find("o", 10, 20)print(result) # 输出:15# 查找不存在的子字符串result = text.find("xyz")print(result) # 输出:-1注意:find() 函数的索引是从 0 开始的。


