Python之路 day2 字串函數

來源:互聯網
上載者:User

標籤:import   其他   重複   sys   ide   斷行符號   列表   使用者輸入   格式   

  1 #Author:ersa  2   3 name = "ersa"  4   5 #首字母大寫capitalize()  6 print(name.capitalize())  7   8 name = "my name is ersa"  9 #字串中 子串 重複的次數 10 print(name.count("a")) 11  12 #center() 字串列印輸出在行中間,並指定列印長度,不夠可用其他字元補充 13 print(name.center(50,‘-‘)) 14  15 #endswith() 判斷字串以什麼結尾?輸出 True 或 False 16 print(name.endswith("sa")) 17  18 #expandtabs()指定 tab 鍵轉換為多個空格 19 name = "my \tname is ersa" 20 print(name.expandtabs(tabsize=20)) 21  22 #find()尋找字串所在的位置 23 print(name.find("y")) 24  25 #字串可以直接被當做列表使用 26 print(name[name.find("name"):]) 27  28 name = "my name is {name} an i am {year} old" 29 #format()格式化輸出 30 print(name.format(name="ersa",year=33)) 31  32 #format_map() 參數可以是dict 33 print(name.format_map({‘name‘:‘ersa‘,‘year‘:22})) 34  35 #isalnum()是否是阿拉伯數字 36 print(‘123‘.isalnum())  #True 37 print(‘123#$‘.isalnum())  #False 38 print(‘ab123‘.isalnum())  #True 39  40 #isalpha()大小寫字母 41 print(‘abcA‘.isalpha())  #True 42  43 #isdecimal() 是否是十進位數 44 print(‘123‘.isdecimal())    #True 45  46 #isdigit() 是否是數字 47 print(‘123‘.isdigit())  #True 48  49 #isidentifier() 是否是合法的標識符(變數名) 50 print(‘ab1‘.isidentifier()) #True 51 print(‘1ab1‘.isidentifier()) #False 52  53 #islower() 是否是小寫字母 54 print(‘abC‘.islower())  #False 55  56 #isupper() 是否大寫 57 print(‘My Name ‘.isupper())   #False 58  59  60 #isnumeric()是否是數字 61 print(‘3.3‘.isnumeric())    #False 62 print(‘33‘.isnumeric()) #True 63  64 #isspace() 是否是空格 65 print(‘ ‘.isspace())    #True 66 print(‘a ‘.isspace())   #False 67  68 #istitle() 是否標題 69 print(‘my name ‘.istitle())   #False 70 print(‘My Name ‘.istitle())   #True 71  72 #isprintable() 是否可列印 73 print(‘a‘.isprintable())    #True tty fiel,drive file不可列印 74  75 #join() 把使用者輸入的參數當作一條命令交給os.system 來執行 76 #import os,sys 77 #os.system(‘‘.join(sys.argv[1:])) 78  79 print(‘+‘.join([‘1‘,‘2‘,‘3‘])) 80  81 print(name.ljust(50,‘*‘)) 82 print(name.rjust(50,‘-‘)) 83  84 #大小寫轉換 85 print(‘Ersa‘.lower()) 86 print(‘Ersa‘.upper()) 87  88 #去除字串中前後的斷行符號換行空格符 89 print(‘\nErsa‘.lstrip()) 90 print(‘Ersa\n‘.rstrip()) 91 print(‘     Ersa\n‘.strip()) 92  93 print(‘------‘) 94  95 #替換對應字元 96 p = str.maketrans("abcdef","123456") 97 print("ersa ma".translate(p)) 98  99 #替換100 print(‘ersa ma‘.replace(‘a‘,‘A‘,1))101 print(‘ersa ma‘.replace(‘a‘,‘A‘))102 103 #尋找 --> 找最右邊的 對應的下標返回104 print(‘ersa ma‘.rfind(‘a‘))105 106 #指定分隔字元107 print(‘ersa ma‘.split(‘s‘))108 print(‘1+2+3+4‘.split(‘+‘))109 print(‘1+2\n+3+4‘.splitlines())110 111 #字串轉大寫112 print(‘ersa ma‘.swapcase())113 114 print(‘ersa ma‘.title())115 116 #zfill 用0填充117 print(‘ersa ma‘.zfill(10))

 

Python之路 day2 字串函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.