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

339
2024/5/13 17:04:03
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: python如何导入自己写的文件