python string type bytes type bytearray type

  • 2020-06-15 09:46:26
  • OfStack

1. python3 makes a distinction between text and binary data. Text is Unicode encoded, str type, for display. The base 2 type is the bytes type for storage and transport. bytes is the sequence of byte and str is the sequence of unicode.

str type:


 >>> s = u' hello '
 >>> s
 ' hello '
 >>> type(s)
 <class 'str'>

bytes type:


 >>> b = b'abc'
 >>> b
 b'abc'
 >>> type(b)
 <class 'bytes'>

2. Conversion relationship between str and bytes: str-- > encode()-- > bytes-- > decode()-- > str

Conversion mode 1: encode(), decode()


 >>> a = u' hello '
 >>> b = a.encode('utf-8')
 >>> b
 b'\xe4\xbd\xa0\xe5\xa5\xbd'
 >>> type(b)
 <class 'bytes'>
 >>> new_a = b.decode('utf-8')
 >>> new_a
 ' hello '
 >>> type(new_a)
 <class 'str'>

Conversion mode 2: bytes(), str()


 >>> a = u' hello '
 >>> b= bytes(a, encoding='utf-8')
 >>> b 
 b'\xe4\xbd\xa0\xe5\xa5\xbd'
 >>> type(b)
 <class 'bytes'>
 >>> new_a = str(b, encoding='utf-8')
 >>> new_a
 ' hello '
 >>> type(new_a)
 <class 'str'>

3. bytearray type

The bytearray class is range 0 < = x < One variable sequence of 256.

The optional source parameter can be initialized in several different ways:

The & # 8226; If it is a string, you must also give the encoding (and optional error) argument; bytearray() then converts the string to bytes using str.encode ().
The & # 8226; If it is 1 integer, the array will have this size and will be initialized with null bytes.
The & # 8226; If it is an object that conforms to the buffer interface, the object's read-only buffer is used to initialize the byte array.
The & # 8226; If it is iterative, it must be range 0 < = x < An iteration of 256 integers, which is used as the initial contents of the array
The & # 8226; If there are no arguments, an array of size 0 is created.

When the source parameter is 1 string:


 >>> b = bytearray(u' hello ', encoding='utf-8')
 >>> b
 bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd')
 >>> type(b)
 <class 'bytearray'>

When the source parameter is 1 integer:


 >>> b = bytearray(5)
 >>> b
 bytearray(b'\x00\x00\x00\x00\x00')
 >>> type(b)
 <class 'bytearray'>

When the source parameter is an iteratable object, the elements of that iterated object must all conform to 0 < = x < 256:


 >>> b = bytearray([1, 2, 3, 4, 255])
 >>> b
 bytearray(b'\x01\x02\x03\x04\xff')
 >>> type(b)
 <class 'bytearray'

4. Differences between bytes and bytearray

bytes is immutable, the same as str. bytearray is variable, same as list.


 >>> b = bytearray()
 >>> b
 bytearray(b'')
 >>> b.append(10)
 >>> b
 bytearray(b'\n')
 >>> b.append(100)
 >>> b
 bytearray(b'\nd')
 >>> b.remove(100)
 >>> b
 bytearray(b'\n')
 >>> b.insert(0, 150)
 >>> b
 bytearray(b'\x96\n')
 >>> b.extend([1, 3, 5])
 >>> b
 bytearray(b'\x96\n\x01\x03\x05')
 >>> b.pop(2)
 1
 >>> b
 bytearray(b'\x96\n\x03\x05')
 >>> b.reverse()
 >>> b
 bytearray(b'\x05\x03\n\x96')
 >>> b.clear()
 >>> b
 bytearray(b'')

5. bytes and bytearray conversion


 >>> b = b'abcdef'
 >>> bay = bytearray(b)
 >>> bay
 bytearray(b'abcdef')
 >>> b = bytes(bay)
 >>> b
 b'abcdef'

6. bytearray and str conversion


 >>> a = 'abcdef'
 >>> b = bytearray(a, encoding='utf-8')
 >>> b
 bytearray(b'abcdef')
 >>> a = b.decode(encoding='utf-8')
 >>> a
 'abcdef'

conclusion

Above is a detailed explanation of this site to you python string type bytes type bytearray type, hope to help you, if you have any questions welcome to leave a message, this site will promptly reply you!


Related articles: