What Python's bytes type is used for? What Python's bytes type is used for? python-3.x python-3.x

What Python's bytes type is used for?


Possible duplicate of what is the difference between a string and a byte string

In short, the bytes type is a sequence of bytes that have been encoded and are ready to be stored in memory/disk. There are many types of encodings (utf-8, utf-16, windows-1255), which all handle the bytes differently. The bytes object can be decoded into a str type.

The str type is a sequence of unicode characters. The str needs to be encoded to be stored, but is mutable and an abstraction of the bytes logic.

There is a strong relationship between str and bytes. bytes can be decoded into a str, and strs can be encoded into bytes.

You typically only have to use bytes when you encounter a string in the wild with a unique encoding, or when a library requires it. str , especially in python3, will handle the rest.

More reading here and here