您好,欢迎来到要发发知识网。
搜索
您的当前位置:首页python有哪些字符串查找类方法

python有哪些字符串查找类方法

来源:要发发知识网

1、find和rfind查找字符串首次和最后一次出现的位置,如果不存在则返回-1。

s = "bird,fish,monkey,rabbit"
s.find('fish') #5
s.rfind('b') #20
s.find('tiger') #-1

2、index和rindex查找时,不存在则抛出异常。

s = "bird,fish,monkey,rabbit"
s.index('bird') #0
s.rindex('i') #21
s.index('tiger') #指定字符串不存在 substring not found

3、count返回一个字符串在另一个字符串中出现的次数,不存在则返回0。

s = "bird,fish,monkey,rabbit"
s.count('bird') # 1
s.count('b') # 3
s.count('tiger') #0

以上就是python字符串查找类方法的介绍,希望对大家有所帮助。更多Python学习指路:

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务