python資料類型之字串(二)

來源:互聯網
上載者:User

標籤: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資料類型之字串(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.