zoukankan      html  css  js  c++  java
  • Python字符串常用方法

    Python提供了很多对字符串的操作的方法,罗列经常使用到的方法:

    str='Hello'
    #首字母变大写
    print str.capitalize()
    #内容居中
    print str.center(30,'=')
    #子序列的个数(字母在字符串中出现了几次)
    print str.count('l')
    #是否已什么结尾
    print str.endswith('o')
    #是否已什么开始
    print str.startswith('H')
    #处理tab键
    str2='Hello	999'
    print str2.expandtabs()
    #寻找子序列位置,没有找到返回-1,返回了是1
    print str.find('a')
    #寻找子序列的位置,没有找到就报错
    print str.index('e')
    #字符串的格式化输出,也就是占位符
    age=20
    name='wuya'
    print 'name is {0},and age is {1}'.format(name,age)
    #判断是否是字母和数字
    print str.isalnum()
    #判断是否是字母
    print str.isalpha()
    #判断是否是数字
    print str.isdigit()
    #判断是否小写
    print str.islower()
    #判断是否有空格
    print str.isspace()
    #判断是否是大写
    print str.isupper()
    #判断是否是标题-->首字母大写,可以理解为大写
    print str.istitle()
    #.join连接字符串
    s1=['appium','selenium','android','ios']
    print '***'.join(s1)
    #使用.join()把列表转为字符串
    print ','.join(s1)
    #字符串转为列表
    a='a b c'
    print a.split(' ')
    #移除空白
    s3=' hello'
    print s3.strip()
    #移除左側空格
    s4=' hello'
    print s4.lstrip()
    #移除右边空格
    s5='world '
    print s5.rstrip()
    #字符串变小写
    print str.lower()
    #分割字符串,分割后就是元组
    s='wuya is python'
    print s.partition('is')
    #替换字符串
    print s.replace('wuya','selenium')
    #rfind()从右向左找
    print s.rfind('wuya')
    #bytes可以把字符串转成字节
    str5='无涯'
    print bytes(str5)
  • 相关阅读:
    编程爱好者论坛第六次编程比赛题目
    google china code jam round2 div1 1000分题
    ogl2dlibsdl cvs repository @ cosoft
    偶尔也提一个游戏点子:宇宙碰撞
    今天看到一些笑话...
    google china code jam入围赛的一个题
    用Regular expression寻找源代码中的汉字字符串
    生成函数理论初步
    Komodo 的中文支持
    “豪华版”的USB延长线
  • 原文地址:https://www.cnblogs.com/liruxian/p/14210138.html
Copyright © 2011-2022 走看看