# 字符串 ###### tags: `人生苦短,我學Python` ## 字符串編碼轉換 :::success :page_facing_up:在 python3.x 中,默認採用編碼格式為UTF-8 :page_facing_up:在 python 中,有兩種常用的字符串類型,為bytes 和 str。str(Unicode字符)、bytes(二進制數據) :page_facing_up:str(在內存中以Unicode表示) :writing_hand:如果在網路上傳輸,或者保存進硬碟,就需要把 str 傳換成 bytes 類型 ::: ## 使用encode()函數解編碼 :::info ```python= str.encode([encoding = 'utf8'][, error = 'strict']) ``` :heavy_check_mark: str : 表示要轉換的字符串 :heavy_check_mark: encoding='utf8' : 可選參數,用於指定進行轉碼時採用的字符編碼 :heavy_check_mark: error = 'strict' : 用於指定錯誤的處理方式,有ignore, replace, xmlcharrefreplace......等,strict為默認值 ::: * 範例 ```python= ex = "人生苦短,我愛Python" ex = ex.encode('utf8') print(ex) # b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe6\x84\x9bPython' # 使用utf-81編碼,轉換後的二進制數據 ``` ## 使用decode()函數解碼 * 二進制轉換回字符串 ```python= ex = b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe6\x84\x9bPython' print(ex.decode()) # 人生苦短,我愛Python ``` ## 字符串常用操作 :::success :package:在Python的開發過程中,很多時候都要對字符串做相應的處理,比如拼接、擷取、格式化...... ::: ## 拼接字符串 ```python= fir = "人生苦短" sec = "我愛Python" print(fir + " , " + sec) # 人生苦短 , 我愛Python ``` * 一定要字串型態加上字串型態才可以拼起來 ```python= fir = "人生苦短" sec = "我愛Python" num = 55 print(fir + " , "+ sec + num) # TypeError: can only concatenate str (not "int") to str ``` :::success :scream:所以說,一定要轉換成相同的型態 ::: ## 計算字符串長度 :::warning :page_with_curl:在Python中,在默認的情況下,一個漢字佔3個字節,數字及其它基本上都佔1個字節 ::: * 在Python中,有提供len()函數計算長度 ```python= str_test = "人生苦短,我愛Python" print(str_test) # 13 ``` :::warning :warning:然而,在默認的情況下,使用len()函數,所有字符都認為是1字節 ::: * 換成encode()函數 ```python= str_test = "人生苦短,我愛Python" print(len(str_test.encode('utf-8'))) # 25 # 3 * 6 + 7 = 25 ``` ## 擷取字符串 * 用slice(切片)的方式 ```python= str_test = 'abcdefg' print(str_test[0:3:2]) # ac ``` :::warning :warning:如果在超出範圍的情況下,可以用try......except IndexError解決 ::: ## 分割字符串 :::info ```python= str.split(sep, maxsplit) ``` :heavy_check_mark:sep : 用於指定分割字符串,可以包含多個字符 :heavy_check_mark:maxsplit : 可選參數,用於指定分割的次數,如果不指定或者為-1,則分割次數沒有限制,否則返回結果列表元素各數最多為maxsplit+1 ::: * 舉例 ```python= str_test = "abc \ndefg" str_test.split() print(str_test) # abc # defg ``` * 附贈 ```python= # 在某些情況下,需要分開進行輸入 nums = list(map(int, input().split())) print(nums) # input 0 1 2 3 4 5 6 7 8 9 # output [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ``` ## 合併字符串 :::warning ```python= str_new = string.join(iterable) ``` ::: * 範例 ```python= str_test = ["a", "b", "c"] print("-".join(str_test)) # a-b-c ``` ## 檢索字符串 * 使用count()函數 ```python= str_test = "aaaabb" print(str_test.count("a")) # 4 ``` * 使用find()函數 ```python= # 返回首次出現的下標 str_test = "aaaabb" print(str_test.find("b")) # 4 ``` * 使用index()函數 ```python= # 返回首次出現的下標 str_test = "aaaabb" print(str_test.index("b")) # 4 ``` * 使用startwith()函數 ```python= # 確認開頭首字母 str_test = "abcabcabc" print(str_test.startswith("b")) # False ``` * 使用endswith()函數 ```python= # 確認末尾字母 str_test = "abcabcabc" print(str_test.endswith("c")) # True ``` ## 字母大小寫轉換 * 使用lower()函數全部轉小寫 ```python= str_test = "wsdjahAKSJD" print(str_test.lower()) # wsdjahaksjd ``` * 使用upper()函數全部轉大寫 ```python= str_test = "wsdjahAKSJD" print(str_test.upper()) # WSDJAHAKSJD ``` ## 去除字符串中的空格和特殊字符 :::warning :warning:可以使用strip()函數去除前後的空格 :ok_hand:當然還有lstrip()和rstrip()...... :warning:這裡的特殊字符是指\t、\r、\n等 ::: * 使用strip()函數 ```python= # 掐頭去尾 str_test = " ssd " print(str_test) # ssd print(str_test.strip()) # ssd ``` * lstrip()與rstrip()的概念一樣 ```python= # l => left # r => right str_test = " ssd " print(str_test.lstrip()) # ssd print(str_test.rstrip()) # ssd ``` ## 格式化字符串 * 使用format()函數 ```python= # str.format(args) print("{} {} {:.5f}".format(50, "中", 20.5555556)) #c 50 中 20.55556 ``` {%hackmd S1DMFioCO %}