標籤:title tab slow wds imp partition nbsp keyword 填充
續接上文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 keyword2 print(keyword.iskeyword("def"))3 test = "def"4 v = test.isidentifier()5 print(v) 結果:
TrueTrue
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)
結果:
FalseAdsjdjksdskd JdfksdslkdjFalse
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********************************feng0000000000000000feng
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 fengFalse 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)
結果:
FalseFalse
14.swapcase:大小寫轉換
1 test = "feng"2 v = test.swapcase()3 print(v)
結果:
FENG
好了,這就是差不多所有常用的字串常用功能
python資料類型之字串(二)