Linux 拨号vps windows公众号手机端

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

lewis 9年前 (2016-08-31) 阅读数 11 #程序编程
文章标签 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
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门