1 # ----------- 首字母大寫 ---------- 2 test = "alex is a man" 3 v = test.capitalize() 4 print(v): Alex is a man 1 # ----------- 轉換全部字元串為小寫 ---------- 2 te... ...
1 # ----------- 首字母大寫 ---------- 2 test = "alex is a man" 3 v = test.capitalize() 4 print(v): Alex is a man
1 # ----------- 轉換全部字元串為小寫 ---------- 2 test = "aLex is A man" 3 V1 = test.casefold() #更加強大,可以處理其他語言體系 4 print(V1) # alex is a man 5 V2 = test.lower() #只處理英文字元 6 print(V2) # alex is a man
1 # ----------- 設置寬度,並將內容居中 ---------- 2 # 20 代表總長度,如果小於字元串本身長度,則忽略 3 # '+' 表示填充的內容,預設為填充空格,只能為單字元(支持中文) 4 test = "aLex is A man" 5 V1 = test.center(20) 6 print('*'+V1+'*') # * aLex is A man * 7 V2 = test.center(20,'+') 8 print('*'+V2+'*') # *+++aLex is A man++++*
1 # ----------- 去字元串中尋找子序列出現的次數 ---------- 2 # 從第5個位置(包括5)開始往後找,預設為從0找起 3 # 直到第14個位置(不包括14)結束,預設找到末尾 4 test = "aLexisAmanAlexALex" 5 V1 = test.count('ex') 6 print(V1) # 3 7 V2 = test.count('ex',5, 14) 8 print(V2) # 1
1 # ----------- 判斷是否以特定字元串結尾/開始 ---------- 2 test = "aLex is A man" 3 V1 = test.endswith('a') 4 print(V1) # False 5 V2 = test.endswith('an') 6 print(V2) # True 7 V1 = test.startswith('a') 8 print(V1) # True 9 V2 = test.startswith('an') 10 print(V2) # False
1 # ----------- 從開始往後找,找到第一個後,獲取其位置 ---------- 2 # 返回第一個找到的字元串下標,找不到則返回-1 3 # 對於查找區間滿足左閉後開的原則 4 test = "aLexaLexaLex" 5 V1 = test.find("ex") 6 print(V1) # 2 7 V2 = test.find("ex",4,7) 8 print(V2) # -1 9 V3 = test.find("ex",4,8) # 4<= 查找位置<8 10 print(V3) # 6
1 # ----------- 格式化1,將字元串中的占位符替換為指定的值 ---------- 2 # 按照占位符名稱替換 3 test = "I am {name},age {a}" 4 print(test) # I am {name},age {a} 5 V1= test.format(name='Alex',a=19) 6 print(V1) # I am Alex,age 19 7 8 # ----------- 格式化2,將字元串中的占位符替換為指定的值 ---------- 9 # 按照數字順序替換 10 test = "I am {0},age {1}" 11 print(test) # I am {0},age {1} 12 V1= test.format('Alex',19) 13 print(V1) # I am Alex,age 19
1 # ----------- 格式化3,將字元串中的占位符替換為指定的值 ---------- 2 # 按照占位符名稱替換, 字典鍵值對方式傳值 3 test = "I am {name},age {a}" 4 print(test) # I am {name},age {a} 5 V1= test.format_map({"name":"alex","a":19}) 6 print(V1) # I am Alex,age 19
1 # ----------- 從開始往後找,找到第一個後,獲取其位置 ---------- 2 # 返回第一個找到的字元串下標,找不到則程式報錯,終止運行 (與find函數的區別) 3 # 對於查找區間滿足左閉後開的原則 4 test = "aLexaLexaLex" 5 V1 = test.index("ex") 6 print(V1) # 2 7 V2 = test.index("8") # 程式報錯 8 print(V2)
1 # ----------- 字元串中是否只包含字母和數字 ---------- 2 # 如果只包含字母和數字,則返回True 3 test = "Alex123+124" 4 V1 = test.isalnum() 5 print(V1) # False 6 test = "Alex123124" 7 V2 = test.isalnum() 8 print(V2) # True
1 # ----------- 分組格式化函數 ---------- 2 # 按照每n個字元分組,碰到\t時使用空格補全n個字元空間 3 test = "123\t456789\t12345" 4 test1 = "1234567891234567891234567" 5 V1 = test.expandtabs(6) 6 print(V1) # 123 456789 12345 7 print(test1) # 1234567891234567891234567 8 test = "Username\tEmail\tPassword\nPeter\[email protected]\t1234\nPeter\[email protected]\t1234\nPeter\[email protected]\t1234" 9 V1 = test.expandtabs(20) 10 print(V1) 11 # Username Email Password 12 # Peter [email protected] 1234 13 # Peter [email protected] 1234 14 # Peter [email protected] 1234