# A demonstration of bytes
a = b"h\x65llo"
print(type(a))
print(list(a))
print(a)<class 'bytes'>
[104, 101, 108, 108, 111]
b'hello'
bytes and strbytes
str
<class 'bytes'>
[104, 101, 108, 108, 111]
b'hello'
<class 'str'>
['a', '̀', ' ', 'p', 'r', 'o', 'p', 'o', 's']
à propos
str instance does not have an associated binary encoding
encode string methodbytes instance does not have an associated text encoding
decode bytes methodstr type
'foo'
'bar'
'foo'
'bar'
bytes and str seem to behave the same way
bytes together, or str together
bytes to str or vice-versab'onetwo'
onetwo
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[5], line 3 1 print(b"one" + b"two") 2 print("one" + "two") ----> 3 print(b"one" + "two") TypeError: can't concat str to bytes
bytes to bytes or str to str
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 3 1 assert b"red" > b"blue" 2 assert "red" > "blue" ----> 3 assert "red" > b"blue" TypeError: '>' not supported between instances of 'str' and 'bytes'
bytes to str for equality always evaluates to False% operator works with format strings for bothb'red blue'
red blue
str to a bytes format string--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[9], line 2 1 blue_str = "blue" ----> 2 print(b"red %s" % blue_str) TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str'
bytes to a str
bytes text is converted to its __repr__ i.e. the string "b'text'"open built-in) expect Unicode strings by default<class 'TypeError'>: write() argument must be str, not bytes
"w" means write which default to text
str.encode with the default text encoding"wb" for write binary<class 'UnicodeDecodeError'>: 'utf-8' codec can't decode byte 0xf1 in position 0: invalid continuation byte
"r" or read mode
bytes.decode)"rb" or read bytes modeimport os
try:
# write the binary data out
with open("data.bin", "wb") as f:
f.write(b"\xf1\xf2\xf3\xf4\xf5")
# read the binary data back in
with open("data.bin", "rb") as f:
data = f.read()
assert data == b"\xf1\xf2\xf3\xf4\xf5"
except Exception as e:
print(f"{type(e)}: {e}")
if os.path.exists("data.bin"):
os.remove("data.bin")open function instead
cp1252 is an old windows text encoding"r" mode"rb"import os
try:
# write the binary data out
with open("data.bin", "wb") as f:
f.write(b"\xf1\xf2\xf3\xf4\xf5")
# read the binary data back in under a different text encoding
with open("data.bin", "r", encoding="cp1252") as f:
data = f.read()
print(data)
except Exception as e:
print(f"{type(e)}: {e}")
if os.path.exists("data.bin"):
os.remove("data.bin")ñòóôõ
bytes contains sequences of 8-bit valuesstr contains sequences of Unicode code pointsbytes and str can’t be used together with operators"rb" or "wb" as the opening mode respectively (b for binary)