1. 程式人生 > >Python學習之python基礎week4-2

Python學習之python基礎week4-2

zed byte init spa 字節序 形式 bsp abc odi

1、bytes與bytearray

# bytes:不可變字節序列; bytearray:字節數組,可變數據類型;

(1)bytes定義

class bytes(object):
    """
    (1)bytes(iterable_of_ints) -> bytes
    (2)bytes(string, encoding[, errors]) -> bytes
    (3)bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
    (4)bytes(int) -> bytes object of size given by the parameter initialized with null bytes # 指定字節的bytes,被0填充;
    b=bytes(10) # 創建10個字節的bytes;
    print(b)   # ==> b‘\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00‘; (5)bytes() -> empty bytes object # 創建空bytes;

# 註意:bytes類型,使用b前綴定義;

  只允許基於ASCII使用字符串形式b‘abc9‘表示

  使用16進制表示b‘\x41\x61‘

(2)bytearray定義  

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
# b=bytearray(5)
# print(b) # bytearray(b‘\x00\x00\x00\x00\x00‘)

2、字符編碼

Python學習之python基礎week4-2