• 技术文章 >Python技术 >Python基础教程

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

    小妮浅浅小妮浅浅2021-10-22 10:01:10原创4169

    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学习指路:python基础教程

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

    专题推荐:python 字符串
    品易云
    上一篇:python __dict__的使用注意 下一篇:python字符串分隔类方法的总结

    相关文章推荐

    • python捕获异常的原因• python创建平衡二叉树的方法• python如何配置文件路径• python字典的底层原理• python函数中的形参有几种• python函数实参的四种类型• python变量名的查找方法• python格式化经纬度的方法• python多进程如何优化显示进度条• python多进程中多个参数函数的使用• python中format_map的使用• python zip_longest和zip的比较• python __dict__的使用注意• python有哪些字符串查找类方法

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网