Python内置的字符串处理函数整理

来源:互联网 发布:招商银行数据分析 编辑:程序博客网 时间:2024/04/30 22:18

收集常用的Python 内置的各种字符串处理 函数的使用方法

str='python String function'

  1. 生成字符串变量str='python String function'
     
  2. 字符串长度获取:len(str)
    例:print '%s length=%d' % (str,len(str))
     
  3. 字母处理
    全部大写:str.upper()
    全部小写:str.lower()
    大小写互换:str.swapcase()
    首字母大写,其余小写:str.capitalize()
    首字母大写:str.title()
    print '%s lower=%s' % (str,str.lower())
    print '%s upper=%s' % (str,str.upper())
    print '%s swapcase=%s' % (str,str.swapcase())
    print '%s capitalize=%s' % (str,str.capitalize())
    print '%s title=%s' % (str,str.title())
  4. 格式化相关
    获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
    获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
    获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
    获取固定长度,右对齐,左边不足用0补齐
    print '%s ljust=%s' % (str,str.ljust(20))
    print '%s rjust=%s' % (str,str.rjust(20))
    print '%s center=%s' % (str,str.center(20))
    print '%s zfill=%s' % (str,str.zfill(20))

  5. 字符串搜索相关
    搜索指定字符串,没有返回-1:str.find('t')
    指定起始位置搜索:str.find('t',start)
    指定起始及结束位置搜索:str.find('t',start,end)
    从右边开始查找:str.rfind('t')
    搜索到多少个指定字符串:str.count('t')
    上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
    print '%s find nono=%d' % (str,str.find('nono'))
    print '%s find t=%d' % (str,str.find('t'))
    print '%s find t from %d=%d' % (str,1,str.find('t',1))
    print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
    #print '%s index nono ' % (str,str.index('nono',1,2))
    print '%s rfind t=%d' % (str,str.rfind('t'))
    print '%s count t=%d' % (str,str.count('t'))

  6. 字符串替换相关
    替换old为new:str.replace('old','new')
    替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
    print '%s replace t to *=%s' % (str,str.replace('t', '*'))
    print '%s replace t to *=%s' % (str,str.replace('t', '*',1))

  7. 字符串去空格及去指定字符
    去两边空格:str.strip()
    去左空格:str.lstrip()
    去右空格:str.rstrip()
    去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
    str=' python String function '
    print '%s strip=%s' % (str,str.strip())
    str='python String function'
    print '%s strip=%s' % (str,str.strip('d'))

  8. 按指定字符分割字符串为数组:str.split(' ')
    默认按空格分隔
    str='a b c de'
    print '%s strip=%s' % (str,str.split())
    str='a-b-c-de'
    print '%s strip=%s' % (str,str.split('-'))

  9. 字符串判断相关
    是否以start开头:str.startswith('start')
    是否以end结尾:str.endswith('end')
    是否全为字母或数字:str.isalnum()
    是否全字母:str.isalpha()
    是否全数字:str.isdigit()
    是否全小写:str.islower()
    是否全大写:str.isupper()
    str='python String function'
    print '%s startwith t=%s' % (str,str.startswith('t'))
    print '%s endwith d=%s' % (str,str.endswith('d'))
    print '%s isalnum=%s' % (str,str.isalnum())
    str='pythonStringfunction'
    print '%s isalnum=%s' % (str,str.isalnum())
    print '%s isalpha=%s' % (str,str.isalpha())
    print '%s isupper=%s' % (str,str.isupper())
    print '%s islower=%s' % (str,str.islower())
    print '%s isdigit=%s' % (str,str.isdigit())
    str='3423'
    print '%s isdigit=%s' % (str,str.isdigit())



    Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾。

    # 例1:字符串截取str = '12345678'print str[0:1]>> 1# 输出str位置0开始到位置1以前的字符print str[1:6]>> 23456# 输出str位置1开始到位置6以前的字符num = 18str = '0000' + str(num)# 合并字符串print str[-5:]# 输出字符串右5位>> 00018

    Python 替换字符串使用 变量.replace("被替换的内容","替换后的内容"[,次数]),替换次数可以为空,即表示替换所有。要注意的是使用replace替换字符串后仅为临时变量,需重新赋值才能保存。

    # 例2:字符串替换str = 'akakak'str = str.replace('k',' 8')# 将字符串里的k全部替换为8print str>> 'a8a8a8'# 输出结果

    Python 查找字符串使用 变量.find("要查找的内容"[,开始位置,结束位置]),开始位置和结束位置,表示要查找的范围,为空则表示查找所有。查找到后会返回位置,位置从0开始算,如果每找到则返回-1。

    # 例3:字符串查找str = 'a,hello'print str.find('hello')# 在字符串str里查找字符串hello>> 2# 输出结果

    Python 分割字符串使用 变量.split("分割标示符号"[分割次数]),分割次数表示分割最大次数,为空则分割所有。

    例4:字符分割str = 'a,b,c,d'strlist = str.split(',')# 用逗号分割str字符串,并保存到列表for value in strlist:# 循环输出列表值    print value>> a# 输出结果>> b>> c>> d

0 0
原创粉丝点击