• Python 字符串操作


    基础操作字符串如下:

    In [10]: sStr="Action {speak}	 louder than {words}"
    

    首字母大写

    In [12]: print(sStr.capitalize())
    Action {speak}   louder than {words}

    查找字符,下标从 0 开始:

    In [13]: print(sStr.index('louder'))
    16
    In [14]: print(sStr.index('c'))
    1

    字符串切片操作,通过find:

    In [38]: print(sStr[sStr.find("th"):])
    than {words} 

      

    清除字符串两端字符:

    In [33]: print("
     
    
    
     
     Action Speack".strip("
    	 "))
    Action Speack
    

    是否是数字:

    In [26]: print("123".isdigit())
    True
    

    从左往右填充字符:

    In [24]: print(sStr.ljust(100,"*"))
    Action {speak}   louder than {words}********************************************
    *********************
    

    以什么字符结尾:

    In [23]: print("action speak louder".endswith("der"))
    True

    列表连接字符串:

    In [18]: list=["action","speak","louder"]
    In [19]: print(' '.join(list))
    action speak louder
    

     tab键扩展:

    In [15]: print(sStr.expandtabs(50))
    Action {speak}                                     louder than {words}
    
    In [16]: print(sStr.expandtabs(tabsize=50))
    Action {speak}                                     louder than {words}
    

     查看某个字符或者sub字符串在字符串出现的次数:

    In [43]: print("str.count",sStr.count('o'))
    str.count 3
    

    如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False

    In [44]: print(sStr.isalpha())
    False
    
    In [45]: print("abc".isalpha())
    True
    

      

     

  • 相关阅读:
    Dubbo简介
    Centos之关机和重启命令
    VirtualBox中CentOS7.2 网络配置(固定IP+联网)
    c#Post方法封装处理
    C# 异步方法处理
    将XMLrequest 改写成fetch
    AsyncCallback
    Promise
    FETCH
    HTML DOM Event 对象
  • 原文地址:https://www.cnblogs.com/fuGuy/p/8026296.html
Copyright © 2020-2023  润新知