1. Introduction
The bytes type was introduced in python3. In Python 3.0, byte string type (bytes) is introduced and named bytes, which represents binary data.
In Python3, the bytes type represents an immutable binary sequence.
2. The bytes data type in python3
1. Create the bytes type
There are several ways to create a bytes type:
(1) Created using literal syntax.
(2) You can use the bytes() function to convert other types of objects to the bytes type.
Let’s briefly learn the creation of bytes type, access and index, and the conversion between bytes type and string.
(1) Created using literal syntax. Create as follows:
b = b"hello python"
(2) Created using the byte() constructor, here is to convert other types of objects to bytes type using the bytes() function.
Give an example:
b1 = bytes([10]) #Convert numbers into byte stringsb2 = bytes([1, 2, 3]) #Convert a list of numbers into a byte stringb3 = bytes((1, 2, 3)) #Convert numeric tuples into byte strings b4 = bytes('a', encoding='utf-8') #Convert single characters to byte stringb5 = bytes("python", encoding='utf-8') #Convert a string to a byte string
Defining the bytes type through the byte() method, there are two possibilities for display: display in binary format, or display in string format:
Displayed in binary format
When the element value range of bytes is outside the range [32, 126], it will be displayed in binary format, as shown below:
b1 = bytes([1, 2, 3, 4, 5]) print(b1) #Output b'\x01\x02\x03\x04\x05'
Display in string format
When the element value range of bytes is outside the range [32, 126], it will be displayed in binary format, as shown below:
b2 = bytes([32, 33, 34, 126]) print(b2) #Output b' '!'~'
2. Access and indexing of bytes
The bytes object is an immutable sequence, similar to a string object. The bytes object can be accessed or intercepted through indexes and slices.
b = bytes("hello python", 'utf-8') print(b[0]) #Output 104print(b[2:5]) #Output b'llo'print(b[2:]) #Output b'llo python'print(b[:3]) #Output b'hel'
3. Conversion of byte string bytes and string str
Convert the bytes type to a string type, for example:
b = bytes("python", 'utf-8') s = ('utf-8') print(s) #Output python
Convert a string to a bytes type, for example:
s = "hello python" b = ('utf-8') print(b) #Output b'hello python'
4. Modify the bytes object
The bytes object is immutable and cannot directly modify the elements in it. If you need to modify it, you can convert it to the bytearray type first and then modify it. Here are examples:
b1 = bytes("hello world", 'utf-8') b2 = bytearray(b1) b2[0] = 72 print(b2) #Output:bytearray(b'Hello world')
5. Delete the bytes object
Use the del() method to delete the bytes object, for example:
b1 = bytes("hello world", 'utf-8') del b1
I've just learned about the byte type here for now.
This is the article about the implementation example of bytes data type in python3. For more related content of python3 bytes data type, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!