续接上文
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 # \t 制表符 2 # \n 换行符 3 test = "ddfer\thuyb" 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:去除左右空白,\t,\n
1 test = " \nfeng " 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\nsdfdfg\ncadewefq\nsd" 2 v4 = test1.splitlines(True) 3 print(v4)
结果:
[‘wdsdsfd\n‘, ‘sdfdfg\n‘, ‘cadewefq\n‘, ‘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
好了,这就是差不多所有常用的字符串常用功能
原文地址:https://www.cnblogs.com/fengpiaoluoye/p/9350762.html
时间: 2024-10-12 05:31:49