• python数据类型之字符串(二)


    续接上文

    1.isdecimal,isgigit,isnumeric:当前输入是否为数字

    
    
    1 test = "12"

    1
    v1 = test.isdecimal() 2 v2 = test.isdigit() 3 v3 = test.isnumeric() 4 print(v1, v2)

    结果:

    True True

    2.isidentifier:判断是否为标识符

    1 import keyword
    2 print(keyword.iskeyword("def"))
    3 test = "def"
    4 v = test.isidentifier()
    5 print(v)

     结果:

    True
    True

    3.isprintable:是否存在不可显示的字符

    1 # 	  制表符
    2 # 
      换行符
    3 test = "ddfer	huyb"
    4 v = test.isprintable()
    5 print(v)

    结果:

    False

    4.isspace:判断是否全部是空格

    1 test = " "
    2 v = test.isspace()
    3 print(v)

    结果:

    True

    5.istitle:判断是否为标题

    1 test = "Adsjdjksdskd jdfksdslkdj"
    2 v1 = test.istitle()
    3 print(v1)
    4 v = test.title()
    5 print(v)
    6 v2 = test.istitle()
    7 print(v2)

    结果:

    False
    Adsjdjksdskd Jdfksdslkdj
    False

    6.join:将字符串中的每一个元素按照指定分隔符进行拼接

    1 test = "你是风儿我是沙"
    2 print(test)
    3 t = ' '
    4 v = t.join(test)
    5 v1 = '_'.join(test)
    6 print(v)
    7 print(v1)

    结果:

    你是风儿我是沙
    你 是 风 儿 我 是 沙
    你_是_风_儿_我_是_沙

    7.ljust,rjust,zfill:对字符串指定大小填充字符

    1 test = "feng"
    2 v = test.ljust(20,"*")
    3 v1 = test.rjust(20,"*")
    4 v2 = test.zfill(20)
    5 print(v)
    6 print(v1)
    7 print(v2)

    结果:

    feng****************
    ****************feng
    0000000000000000feng

    8.islower,lower,isupper,upper:判断是否全部大小写和转换为大小写

    1 test = "Feng"
    2 v1 = test.islower()
    3 v2 = test.lower()
    4 print(v1,v2)
    5 v3 = test.isupper()
    6 v4 = test.upper()
    7 print(v3,v4)

    结果:

    False feng
    False FENG

    9.lstrip,rstrip,strip:去除左右空白, ,

    1 test = " 
    feng "
    2 v = test.lstrip()
    3 v1 = test.rstrip()
    4 v2 = test.strip()
    5 print(v,v1,v2)

    结果:

    feng   
    feng feng

    10.translate:将对应的字符进行转换

    1 v = "saevdds;gfuiuyv;sdiujsd"
    2 m = str.maketrans("aeiou", "12345")
    3 new_v = v.translate(m)
    4 print(new_v)

    结果:

    s12vdds;gf535yv;sd35jsd

    11.partition,rpartition,split,rsplit:按照指定字符进行分割

    1 test = "testuufuskjksdkd"
    2 v = test.partition('s') # 最多分割成三分
    3 v1 = test.rpartition('s')
    4 v2 = test.split('s',2)
    5 v3 = test.rsplit('s',3)
    6 print(v, v1, v2, v3)

    结果:

    ('te', 's', 'tuufuskjksdkd') ('testuufuskjk', 's', 'dkd') ['te', 'tuufu', 'kjksdkd'] ['te', 'tuufu', 'kjk', 'dkd']

    12.splitlines:分割,只能根据,True,False:是否保留换行

    1 test1 = "wdsdsfd
    sdfdfg
    cadewefq
    sd"
    2 v4 = test1.splitlines(True)
    3 print(v4)

    结果:

    ['wdsdsfd
    ', 'sdfdfg
    ', 'cadewefq
    ', 'sd']

    13.startswith,endswith:以xxx开头,以xx结尾

    1 test = "backend 1.1.1.1"
    2 v = test.startswith("a")
    3 print(v)
    4 v1 = test.endswith('a')
    5 print(v1)

    结果:

    False
    False

    14.swapcase:大小写转换

    1 test = "feng"
    2 v = test.swapcase()
    3 print(v)

    结果:

    FENG

    好了,这就是差不多所有常用的字符串常用功能

    每天学习新的知识,会让自己更加充实
  • 相关阅读:
    解决CHM文件不能浏览的问题
    SAS宏功能(下)
    python一句话求素数
    SAS与DBMS系统(SqlServer)通信
    CSS3圆角
    水平居中和垂直居中
    滚动条的控制
    快来给你的桌面加一只可爱的蟑螂吧 那个人
    自己用的PHP缓存类
    jquery 全反选
  • 原文地址:https://www.cnblogs.com/fengpiaoluoye/p/9350762.html
Copyright © 2020-2023  润新知