扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python中find()的使用方法是什么

扬州沐宇科技
2024-04-19 17:04:03
python

在Python中,find()方法用于查找字符串中是否包含指定的子字符串。该方法会返回子字符串在原始字符串中第一次出现的索引位置,如果没有找到则返回-1。

string = "Hello, World!"
index = string.find("World")
print(index)  # 输出:7

index = string.find("Python")
print(index)  # 输出:-1

find()方法还可以指定起始和结束索引,以限制搜索范围:

string = "Hello, World!"
index = string.find("o", 5)
print(index)  # 输出:7

index = string.find("o", 5, 8)
print(index)  # 输出:-1

扫码添加客服微信