python中index()与find()的差异
发布时间:2020-12-20 10:16:12 所属栏目:Python 来源:网络整理
导读:发现了python中的index()和find()实现的功能相似,去百度发现还是有不一样的。 先来个正常的 msg = "mynameishelie"print(msg.index("m"))print(msg.find("m")) 输出结果为: 0 0 Process finished with exit code 0 继续 msg = "mynameishelie"print(msg.in
发现了python中的index()和find()实现的功能相似,去百度发现还是有不一样的。 先来个正常的 msg = "mynameishelie" print(msg.index("m")) print(msg.find("m")) 输出结果为: 0 Process finished with exit code 0 继续 msg = "mynameishelie" print(msg.index("L")) print(msg.find("L"))
Traceback (most recent call last): Process finished with exit code 1 好了,下面来找下index的语法使用: def index(self,sub,start=None,end=None): # Like S.find() but raise ValueError when the substring is not found. def find(self,end=None): # Return the lowest index in S where substring sub is found,# such that sub is contained within S[start:end]. Optional# arguments start and end are interpreted as in slice notation.# Return -1 on failure.和index()不同的是find()在找不到substring时不会抛出异常,而是会返回-1,因此不会影响程序的执行。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |