zoukankan      html  css  js  c++  java
  • 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
    

      

     

  • 相关阅读:
    个人7天冲刺计划
    团队项目开发个人周计划
    满足NABC的软件创意
    关于二维数组相邻元素和的最大值问题的探讨
    敏捷软件开发综述
    关于开发方法的综述
    二维数组的子数组求最大值问题
    电梯调度实施
    小组作业提交报告
    模拟卷链接
  • 原文地址:https://www.cnblogs.com/fuGuy/p/8026296.html
Copyright © 2011-2022 走看看