
1、首次和最后一次出现find和rfind搜索字符串的位置,如果不存在,则返回-1。
s="bird,fish,monkey,rabbit"
s.find('fish')#5
s.rfind('b')#20
s.find('tiger')#-12、在搜索index和rindex时,如果不存在,就会抛出异常。
s="bird,fish,monkey,rabbit"
s.index('bird')#0
s.rindex('i')#21
s.index('tiger')#substringnotfound不存在指定字符串3、在另一个字符串中,count返回一个字符串的次数,如果不存在,则返回0。
s="bird,fish,monkey,rabbit"
s.count('bird')#1
s.count('b')#3
s.count('tiger')#0以上是python字符串搜索方法的介绍,希望对大家有所帮助。更多Python学习指导:基础教程python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。