SoFunction
Updated on 2024-11-10

Python3 byte string bytes and byte array bytearray use details

Byte string bytes

Byte strings, also called byte sequences, are immutable sequences that store data in bytes

Byte string representation:

b"ABCD"
b"\x41\x42"
...

Constructor for byte strings:

bytes() creates an empty string of bytes, same as b""

bytes (integer iterable) Creates a string of bytes with an iterable object.

bytes(integer n) generates n strings of bytes with value 0

bytes(string, encoding='utf-8') transcoding

Operations on byte strings: operations with other sequences

+、+=、*、*=
<、<=、>、>=、!=、==
in / not in 
Slicing and Indexing
function (math.):
len
max
min
sum
any
all

Conversion between bytes and str:

str --> bytes use encode

Take a chestnut:

>>> s = 'Hello, world!'
>>> (encoding='utf-8')
b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81'

bytes-->str: using decode

Take a chestnut:

>>> s = b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81'
>>> (encoding='utf-8')
'Hello, world!'

bytearray is the same as bytes, without going into the

End of section!

Above this Python3 of byte string bytes and byte array bytearray the use of detailed is all I share with you, I hope to give you a reference, and I hope you support me more.