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

    Python字符串方法如何使用

    小妮浅浅小妮浅浅2021-09-06 09:31:19原创3567

    1、find方法可以在一个较长的字符串中查找子串

    返回子串所在位置的最左端索引,如果没有找到则返回-1

    a = 'abcdefghijk'
    print(a.find('abc'))                                  #the result : 0
    print(a.find('abc',10,100))                           #the result : 11  指定查找的起始和结束查找位置

    2、join方法是非常重要的字符串方法。

    是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。

    a = ['1','2','3']
    print('+'.join(a))                                    #the result : 1+2+3

    3、split是join的逆方法。

    用来将字符串分割成序列

    print('1+2+3+4'.split('+'))                          #the result : ['1', '2', '3', '4']

    4、strip方法返回去除首位空格。

    print("   test   test    ".strip())                  #the result :“test   test”

    replace方法返回某字符串。

    所有匹配项均被替换之后得到字符串。

    print("This is a test".replace('is','is_test'))     #the result : This_test is_test a test

    以上就是Python字符串方法的使用,希望对大家有所帮助。更多Python学习指路:python基础教程

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

    专题推荐:python字符串
    品易云
    上一篇:Python列表操作方法的整理 下一篇:Python字典常用方法汇总

    相关文章推荐

    • python字符串需要注意的语法问题• python字符串的翻转实现的两种方法• python字符串的拆分与合并• Python字符串有哪几种表示形式• python字符串有几种常见方法• python字符串常用方法有哪些• python字符串如何取值• python字符串格式化的方法整理• python字符串如何简单运算• python字符串索引的用法• python字符串中有哪些方法

    全部评论我要评论

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

  • 取消发布评论
  • 

    Python学习网