1, in the python2.x is not distinguished bytes and str type, in Python3 bytes and STR is differentiated, all operation of str bytes are supported
In Python2
>>> s = "ABCDEFG"
>>> B = S.encode () #或者使用下面的方式
>>> B = B "ABCDEFG"
>>> type (b)
<type ' str ' >
#str和bytes是严格区分的 in Python3
>>> s = "ABCDEFG"
>>> type (s)
<class ' str ' >
>>> B = B "ABCDEFG"
>>> type (b)
<class ' bytes ' >
STR is a text series, Bytes is a byte series
Text is encoded (utf-8,gbk,gb2312, etc.)
BYTE is not encoded
Text encoding refers to how characters use bytes to represent the organization, which is used by default under Linux UTF-8
Conversion-------encoding between 2,bytes and STR
The bytes is converted by str through the Encode method, and Str can be transformed by bytes through the Decode method.
Bytes can be defined by the B prefix
GBK is a double byte, UTF-8 flexible encoding, 1 bytes, 2 bytes, 3 bytes, 4 bytes all have, maximum support 6 byte length, Chinese most is 3 bytes
>>> S = "I am Chinese"
>>> S
' I am a Chinese '
>>> B = S.encode () #进行编码为bytes
>>> b
B ' \xe6\x88\x91\xe6\x98\xaf\xe4\xb8\xad\xe5\x9b\xbd\xe4\xba\xba '
>>> B.decode () #进行解码为字符串
' I am a Chinese '
>>>
If STR is encoded in any format, it needs to decode what format it is encoded in.
>>> S = "I am Chinese"
>>> S
' I am a Chinese '
>>> B = S.encode (' GBK ')
>>> b
B ' \XCE\XD2\XCA\XC7\XD6\XD0\XB9\XFA\XC8\XCB '
>>> b.decode (' GBK ')
' I am a Chinese '
You can use Bin () to convert a 10 binary integer or 16 binary number to 2 binary
>>> Bin (10)
' 0b1010 '
>>>
>>> Bin (0xce)
' 0b11001110 '
Operation of the 3,bytes
The bytes has all the operations of type string, bytes can be converted via STR encode, or it can be defined by prefix b
>>> B = B ' abc '
>>> b
B ' ABC '
>>> B.decode ()
' ABC '
>>> b ' abc '. FIND (b ' C ')
2
>>> Len (' I'm Chinese '). Encode ()) #求bytes的长度
15
>>> b
B ' ABC '
>>> B.hex () #转化为16进制
' 616263 '
>>> bin (616263) #转化为2进制
' 0b10010110011101000111 '
In addition to encode, the STR operation has a corresponding bytes version, but the parameters passed in must be bytes
ByteArray type
ByteArray are mutable, bytes and Str are immutable and are mainly used for image processing
Compared to bytes, insert,append,extend,pop,remove,clear,reverse and other operations are supported, and index operations are enabled
>>> S1 = "Life is short, I learn python! "
>>> S1
' Life is short, I learn python! ‘
>>> B1 = ByteArray (S1.encode ())
>>> B1.decode ()
' Life is short, I learn python! ‘
>>> B1
ByteArray (b ' \XE4\XBA\XBA\XE7\X94\X9F\XE8\X8B\XA6\XE7\X9F\XAD\XEF\XBC\X8C\XE6\X88\X91\XE5\XAD\XA6PYTHON\XEF\XBC \x81 ')
>>> B1[:6] = ByteArray (' Life '. Encode ())
>>> S1
' Life is short, I learn python! ‘
>>> B1
ByteArray (b ' \XE7\X94\X9F\XE5\X91\XBD\XE8\X8B\XA6\XE7\X9F\XAD\XEF\XBC\X8C\XE6\X88\X91\XE5\XAD\XA6PYTHON\XEF\XBC \x81 ')
>>> B1.decode ()
' Life is short, I learn python! ‘
>>>
Original: 53811035
The biggest difference between Python2 and Python3 (coding problem BYTES&STR