zoukankan      html  css  js  c++  java
  • 字符串的多种操作

    #strip

     name='*agon**'

    print(name.strip('*'))

    print(name.lstrip('*'))

    print(name.rstrip('*'))

    #startswith  endswith

    print(name.endswith('SB'))

    print(name.startswith('alex'))

    #replace

    name='ale namx say:I have one tesla,my name is alex'

    print(name.replace('alex','SB',l))

    #format的三种玩法

     res='{} {} {} '.format('egon',18,'male')

     res='{1}{0}{1}'.format('egon',18,'male')

     res='{name}{age}{sex}'.format{sex='male',name='egon',age=18}

    #find,rfind,index,rindex,count

    name='egon say hello'

    print(name,find('o',1,3)) #顾头不顾尾,找不到则返回,-1不会报错,找到了则显示索引

    print(name.index('e',2,4))#同上,但是到不到会报错

    print(name.count('e',1,3))  #顾头不顾尾,如果不指定范围则查到所有

    #split

     name='root:x:0:0::/root:/bin/bash'

    pirnt(name.split(':')) #默认分隔符为空格

    name='C:/a/b/c/d.txt' #只想拿到顶级目录

    print(name.rsplit('/',l))

    name='a/b/c'

    print(name.rsplit('l',l)) #从右开始切分

    #join

    tag=' '

    print(tag.join(['egon','say','hello','world'])) #可迭代对象必须都是字符串

    #center,ljust,rjust,zfill

    name='egon'

    print(name.center(30,'-'))

    print(name.ljust(30,'*')

    print(name.rjust(30,'*'))

    print(name.zfill(50)) #用0填充

    #expandtabs        

    name='egon+hello'       

     print(name)        

    print(name.expandtabs(l)) #lower  upper       

    name='egon'       

    print(name.upper())      

     prnit(namae.lower())

    #captalize,swapcass,title      

    print(name.captalize())  #首字母大写       

    print(name.swapcass())  #大小写翻转       

    mas='egon say hi'       

    print(mas.title())      #每个单词的首字母的大写  

    #is数字系列 #在py3中      

    num1=b'4'  #bytes      

    num2=u'4'  #unicode py3中无需加u     

     num3='四'  #中文数字      

    num4='IV'  #罗马数字 #isdigt:bytes,unicode    

    print(num1.isdigit()) #True      

    print(num2.isdigit()) #True      

    print(num3.isdigit()) #False      

    print(num4.isdigit()) #False

    #isdecimal:uncicode   

    #bytes类型无isdecimal方法      

    print(num2.isdecimal())#True       .

    print(num3.isdecimal())#False      

    print(num4.isdecimal())#False

    #isnumberic:unicode,中文数字  罗马数字    

    #bytes 类型无isnumberic方法    

    print(num2.isnumberic())#True    

    print(num3.isnumberic())#True    

    print(num4.isnumberic())#True

    #3三者不能判断浮点数      

    num5='4.3'    

    print(num5.isdigit)    

    print(num5.isdecimal)    

    print(num5.isnumberic)

    #is其他    print('====>')   

    name='egon 123'   

    print(name.isalnum()) #字符串由字母和数字组成   

    print(name.isalpha())  #字符串只由字母组成   

    print(name.isidentitler())   

    print(name.islower())   

    print(name.isupper())   

    print(name.isspace())   

    print(name.istitle())        

      -

  • 相关阅读:
    Linux下如何查看版本信息
    java单利模式设计
    MIT 2012 分布式课程基础源码解析-底层通讯实现
    MIT 2012分布式课程基础源码解析-事件管理封装
    MIT 2012分布式课程基础源码解析-线程池实现
    MIT 2012分布式课程基础源码解析一-源码概述
    Leetcode按Tag刷题
    网页搜集系统
    c/c++中的各种字符串转换
    gentoo装X服务器时显卡选择
  • 原文地址:https://www.cnblogs.com/1996-11-01-614lb/p/7200502.html
Copyright © 2011-2022 走看看