1、字符串格式化python
一、字符串格式化bash
字符串格式化是拼接字符串的一種手段app
此前學過str.join()和+來拼接str,但難以控制格式ide
str格式化有另種方式printf style 和str.format()ui
二、printf style編碼
從c語言繼承過來的spa
In [2]: s = 'i love %s'
待格式化的字符串,當一個str存在佔位符的時候;3d
佔位符:%加一個格式控制符code
In [3]: s Out[3]: 'i love %s' In [4]: s %('python',) Out[4]: 'i love python' In [5]: s %('python') Out[5]: 'i love python' In [6]: s %'python' Out[6]: 'i love python' In [7]: 'i love %s' %'python' Out[7]: 'i love python'
傳入參數順序的替換佔位符,返回替換後的str,原str不變orm
In [9]: 'i love %s, i am %d' % ('python', 18) Out[9]: 'i love python, i am 18' In [11]: 'i love %s, i am %d' % ('python' 18) File "<ipython-input-11-b6c40f507b33>", line 1 'i love %s, i am %d' % ('python' 18) ^ SyntaxError: invalid syntax In [13]: 'i love %s, i am %d' % ('python',) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-13-b0f8a99953ee> in <module>() ----> 1 'i love %s, i am %d' % ('python',) TypeError: not enough arguments for format string In [14]: 'i love %s, i am %d' % ('python',"xxj") --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-9819bcbd229f> in <module>() ----> 1 'i love %s, i am %d' % ('python',"xxj") TypeError: %d format: a number is required, not str
當佔位符個數和參數個數不匹配的時候,會拋出TypeError
In [25]: 'i love %s, i am %d' % ('python', 18) Out[25]: 'i love python, i am 18' In [26]: 'i love %s, i am %d' % ('python', "xxj") --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-448f3d60565b> in <module>() ----> 1 'i love %s, i am %d' % ('python', "xxj") TypeError: %d format: a number is required, not str In [27]: 'i love %s, i am %d' % (18, 18) # 爲何這裏類型不對,但沒報錯 Out[27]: 'i love 18, i am 18' In [28]: 'i love %s, i am %d' % ([1, 2], 18) Out[28]: 'i love [1, 2], i am 18' In [29]: 'i love %s, i am %d' % (1, 2, 18) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-29-de8412982695> in <module>() ----> 1 'i love %s, i am %d' % (1, 2, 18) TypeError: not all arguments converted during string formatting In [30]: 'i love %s, i am %d' % ((1, 2), 18) Out[30]: 'i love (1, 2), i am 18'
%s:表示str或任意對象,或隱式的調用str()將對象轉化成str
語法格式:
print 「String %format1 %format2 …」 %(variable1, varialbe2, …)
%後面format前面可使用的修飾符,(若是有,則只能按以下順序):
%[(name)][flags][width][.precision]typecode
typecode就是上面的format和圖中的字符
位於括號中的一個屬於後面的字典的鍵名,用於選出一個具體項
flags是下面標誌中的一個或多個:
-:表示左對齊,默認爲右對齊
+:表示包含數字符號,正數也會帶「+」
0:表示一個零填充
width指定最小寬度的數字
.用於按照精度分割字段的寬度
precision指定要打印字符串中的最大字符個數,浮點數中小數點以後的位數,或者整數的最小位數(前面補0);
## %s In [34]: 'I love %s, i am %d' % ("python", 18) Out[34]: 'I love python, i am 18' In [35]: 'I love %-s, i am %d' % ("python", 18) Out[35]: 'I love python, i am 18' In [36]: 'I love %-30s, i am %d' % ("python", 18) Out[36]: 'I love python , i am 18' In [37]: 'I love %30s, i am %d' % ("python", 18) Out[37]: 'I love python, i am 18' In [38]: 'I love %030s, i am %d' % ("python", 18) Out[38]: 'I love python, i am 18' In [39]: 'I love %-030s, i am %d' % ("python", 18) Out[39]: 'I love python , i am 18' In [40]: 'I love %-030.5s, i am %d' % ("python", 18) Out[40]: 'I love pytho , i am 18' ## %d In [49]: 'I love %s, i am %d' % ("python", 18) Out[49]: 'I love python, i am 18' In [50]: 'I love %s, i am %20d' % ("python", 18) Out[50]: 'I love python, i am 18' In [51]: 'I love %s, i am %020d' % ("python", 18) Out[51]: 'I love python, i am 00000000000000000018' In [52]: 'I love %s, i am %-20d' % ("python", 18) Out[52]: 'I love python, i am 18 In [53]: 'I love %s, i am %-20.5d' % ("python", 18) Out[53]: 'I love python, i am 00018 ' In [54]: 'I love %s, i am %-20.6d' % ("python", 18) Out[54]: 'I love python, i am 000018 ## %f In [43]: 'I love %s, i am %f' % ("python", 18) Out[43]: 'I love python, i am 18.000000' In [44]: 'I love %s, i am %20f' % ("python", 18) Out[44]: 'I love python, i am 18.000000' In [45]: 'I love %s, i am %020f' % ("python", 18) Out[45]: 'I love python, i am 0000000000018.000000' In [46]: 'I love %s, i am %-020f' % ("python", 18) Out[46]: 'I love python, i am 18.000000 ' In [47]: 'I love %s, i am %-020.5f' % ("python", 18) Out[47]: 'I love python, i am 18.00000 ' In [48]: 'I love %s, i am %-020.5d' % ("python", 18) Out[48]: 'I love python, i am 00018
printf style 格式化對其它語言,尤爲是c語言轉過來的人,很是容易接受;但並非Python建議使用的方法。
三、str.format()
In [67]: 'I love {}'.format('python') Out[67]: 'I love python'
str.format()使用大括號做爲佔位符
當調用str.format()方法,format()傳入的參數會替換大括號
In [68]: 'I love {}, i am {}'.format('python', 18) Out[68]: 'I love python, i am 18'
format()的參數個數是可變的
In [70]: 'I love {}, i am {}'.format('python', 18) Out[70]: 'I love python, i am 18' In [71]: 'I love {}, i am {}'.format(18, 'python') Out[71]: 'I love 18, i am python' In [72]: 'I love {1}, i am {0}'.format(18, 'python') Out[72]: 'I love python, i am 18' In [73]: 'I love {1}, i am {1}'.format(18, 'python') Out[73]: 'I love python, i am python' In [74]: 'I love {1}, i am {1}'.format(18) # 佔位符中的數字指定的位置參數須要存在 --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-74-9777ef27de22> in <module>() ----> 1 'I love {1}, i am {1}'.format(18) IndexError: tuple index out of range In [75]: 'I love {0}, i am {1}'.format(18) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-75-1381c3981335> in <module>() ----> 1 'I love {0}, i am {1}'.format(18) IndexError: tuple index out of range In [76]: 'I love {0}, i am {0}'.format(18) # 佔位符中的數字能夠屢次調用一個位置參數 Out[76]: 'I love 18, i am 18'
可使用佔位符加數字調用format的位置參數,而且能夠屢次調用同一個位置參數
In [77]: 'I love {lang}, i am {age}'.format(lang='python', age=18) Out[77]: 'I love python, i am 18' In [78]: 'I love {lang}, i am {lang}'.format(lang='python', age=18) Out[78]: 'I love python, i am python' In [79]: 'I love {lang}, i am {lang}'.format(lang='python') Out[79]: 'I love python, i am python' In [81]: 'My name is {0}, i love {lang}, i am {age}'.format('xxj', lang='python', age=18) Out[81]: 'My name is xxj, i love python, i am 18'
能夠在佔位符里加標識符,來使用關鍵字參數
能夠同時支持位置參數和關鍵字參數
佔位符和參數不匹配時,會拋出異常
In [82]: '{} {}'.format(18) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-82-21db8d47c754> in <module>() ----> 1 '{} {}'.format(18) IndexError: tuple index out of range In [83]: '{} {lang}'.format(18) --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-83-afb26cfd80bf> in <module>() ----> 1 '{} {lang}'.format(18) KeyError: 'lang' In [84]: '{1} {2}'.format(0, 1, 2) Out[84]: '1 2' In [85]: '{1} {2}'.format("a", "b", "c") Out[85]: 'b c'
{} 會按照順序使用位置參數
{數字} 會把位置參數當成一個列表args, agrs[i],當i不是args的索引的時候,拋出IndexError
{關鍵字} 會把關鍵字參數當成一個字典kwargs,使用kwargs[k]當k不是kwargs的key時,會拋出KeyError
在python2.6版本中,不能勝率大括號裏的數字或者關鍵字
如何print 大括號呢?
In [90]: '{}'.format(18) Out[90]: '18' In [91]: '{{}}'.format(18) Out[91]: '{}' In [92]: '{{{}}}'.format(18) Out[92]: '{18}' In [93]: '{{{}}}'.format() --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-93-d7108b8b948d> in <module>() ----> 1 '{{{}}}'.format() IndexError: tuple index out of range
2、bytes
一、bytes
bytes是python新引入的type
str是文本序列,bytes是字節序列
文本是有編碼的(utf-8,gbk,GB18030等),字節沒有編碼這種說法
文本的編碼是指,如何使用字節來表示字符
python3 str默認使用utf-8編碼
str的全部操做除了encode,都有隊友bytes的版本,可是傳入的參數也必須是bytes
In [175]: b = b'i love python' In [176]: b.find('o') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-176-03a41f7339b4> in <module>() ----> 1 b.find('o') TypeError: a bytes-like object is required, not 'str' In [177]: b.find(b'o') Out[177]: 3 In [180]: s = '馬哥教育' In [181]: s.encode() Out[181]: b'\xe9\xa9\xac\xe5\x93\xa5\xe6\x95\x99\xe8\x82\xb2' In [182]: s.encode().find(b'\xac') # bytes的操做是按字節來的 Out[182]: 2 In [184]: b Out[184]: b'i love python' In [185]: b.decode() # bytes所特有的方法 Out[185]: 'i love python' In [186]: b.hex() # bytes所特有的方法 Out[186]: '69206c6f766520707974686f6e'
二、str轉爲bytes
In [145]: help(str.encode) Help on method_descriptor: encode(...) S.encode(encoding='utf-8', errors='strict') -> bytes Encode S using the codec registered for encoding. Default encoding is 'utf-8'. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name registered with codecs.register_error that can handle UnicodeEncodeErrors. In [103]: s.encode() # 將str編碼成bytes Out[103]: b'\xe9\xa9\xac\xe5\x93\xa5\xe6\x95\x99\xe8\x82\xb2' # 每3個16進制的數字表示一箇中文 In [106]: type(s.encode) Out[106]: builtin_function_or_method In [107]: type(s.encode()) Out[107]: bytes In [109]: '馬'.encode() Out[109]: b'\xe9\xa9\xac' In [127]: bin(0xe9) # 將16進制轉化爲二進制 Out[127]: '0b11101001' In [128]: bin(0xa9) Out[128]: '0b10101001' In [129]: bin(0xac) Out[129]: '0b10101100' 11101001 10101001 10101100 代碼馬字 In [133]: s.encode() Out[133]: b'\xe9\xa9\xac\xe5\x93\xa5\xe6\x95\x99\xe8\x82\xb2' In [134]: s.encode(gbk) --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-134-8fa822e76cc5> in <module>() ----> 1 s.encode(gbk) NameError: name 'gbk' is not defined In [135]: s.encode("gbk") # 使用不一樣的編碼,所獲得的bytes不一樣 Out[135]: b'\xc2\xed\xb8\xe7\xbd\xcc\xd3\xfd' In [136]: s.encode("GBK") Out[136]: b'\xc2\xed\xb8\xe7\xbd\xcc\xd3\xfd' In [137]: s.encode("utf8") Out[137]: b'\xe9\xa9\xac\xe5\x93\xa5\xe6\x95\x99\xe8\x82\xb2' In [138]: s.encode("utf-8") Out[138]: b'\xe9\xa9\xac\xe5\x93\xa5\xe6\x95\x99\xe8\x82\xb2'
三、bytes轉爲str
In [143]: help(bytes.decode) Help on method_descriptor: decode(self, /, encoding='utf-8', errors='strict') Decode the bytes using the codec registered for encoding. encoding The encoding with which to decode the bytes. errors The error handling scheme to use for the handling of decoding errors. The default is 'strict' meaning that decoding errors raise a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' as well as any other name registered with codecs.register_error that can handle UnicodeDecodeErrors. In [139]: s.encode().decode() # decode()解碼 Out[139]: '馬哥教育' In [140]: s.encode().decode("gbk") # 須要使用編碼時所使用的編碼方式才能正確解碼 --------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) <ipython-input-140-e4970109fa53> in <module>() ----> 1 s.encode().decode("gbk") UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
3、bytearray
一、bytearray
bytearray是bytes的可變版本
str和bytes都是不可變的
In [197]: help(bytearray) Help on class bytearray in module builtins: class bytearray(object) | bytearray(iterable_of_ints) -> bytearray | bytearray(string, encoding[, errors]) -> bytearray | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes | bytearray() -> empty bytes array | | Construct a mutable bytearray object from: | - an iterable yielding integers in range(256) | - a text string encoded using the specified encoding | - a bytes or a buffer object | - any object implementing the buffer API. | - an integer In [206]: bytearray(10) Out[206]: bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') In [207]: bytearray(b"10") Out[207]: bytearray(b'10') In [208]: bytearray(b"abc") Out[208]: bytearray(b'abc') In [209]: bytearray("abc") --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-209-355ddbfdfb18> in <module>() ----> 1 bytearray("abc") TypeError: string argument without an encoding In [210]: bytearray("abc", encoding) --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-210-6baeeb8ceb00> in <module>() ----> 1 bytearray("abc", encoding) NameError: name 'encoding' is not defined In [211]: bytearray("abc", "utf") Out[211]: bytearray(b'abc') In [212]: bytearray("abc", "utf8") Out[212]: bytearray(b'abc') In [213]: bytearray("abc", "utf-8") Out[213]: bytearray(b'abc') In [215]: bytearray([1, 2]) Out[215]: bytearray(b'\x01\x02') In [226]: b = bytearray(12) # bytearry 是可變的 In [227]: b Out[227]: bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') In [228]: b[3] Out[228]: 0 In [229]: b[3]= 5 In [230]: b Out[230]: bytearray(b'\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00')
bytearray相對於bytes來講,多了insert、append、extend、pop、remove、clear reverse方法,而且能夠索引操做
bytearray的insert、append、remove、count的參數必須是int,由於bytearray操做的字節,但python中沒有byte這種類型,能夠用int來表示byte。int必須在0-256這個範圍內