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
Answer from Jtcruthers on Stack OverflowPossible 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
Sockets is a good case for using bytes. Also you should use bytes to read/write binary data, like image or audio, from a file or a web API. The str type is an immutable sequence of character, which is typically UTF8 encoded. Obviously if your data is not characters, then doing the UTF8 encoding on it will be inefficient and could cause bugs.
I'm reading the official docs and all, tried some examples, but still feels like it's just a string.
Then I didn't understand why when I loop through a bytes object and print the current element it comes out as an integer, but when I print the whole content of the object it comes out as string like: b'whatever is in the string', and when I cast something using bytes() it returns a string formatted as hex.
The new bytes type is 3.x only. The 2.x bytes built-in is just an alias to the str type. There is no new type called bytes in 2.x; Just a new alias and literal syntax for str.
Here's the documentation snippet everybody loves:
Python 2.6 adds
bytesas a synonym for thestrtype, and it also supports theb''notation.The 2.6
strdiffers from 3.0’s bytes type in various ways; most notably, the constructor is completely different. In 3.0,bytes([65, 66, 67])is 3 elements long, containing the bytes representingABC; in 2.6,bytes([65, 66, 67])returns the 12-byte string representing thestr()of the list.The primary use of
bytesin 2.6 will be to write tests of object type such asisinstance(x, bytes). This will help the2to3converter, which can’t tell whether 2.x code intends strings to contain either characters or 8-bit bytes; you can now use eitherbytesorstrto represent your intention exactly, and the resulting code will also be correct in Python 3.0.
The bytes type was introduced in Python 3, but what's being discussed in the PEP is a mutable sequence (bytes is immutable) which was introduced in Python 2.6 under the name bytearray.
The PEP clearly wasn't implemented as stated (and it does say that it was partially superseded by PEP 3137) but I think it's only a question of things being renamed, not features missing. In Python 2 bytes is just an alias for str to aid forward compatibility and so is a red-herring here.
Example bytearray usage:
>>> a = bytearray([1,2,3])
>>> a[0] = 5
>>> a
bytearray(b'\x05\x02\x03')