Python: str() to bytes()

1. String para bytes.
data = ""                          #string
data = "".encode()                 #bytes
data = "".encode('utf-8')          #bytes
data = "".encode(encoding='utf-8') #bytes
data = bytes("")                   #bytes
2. bytes para String.
data = b""                              #bytes
data = b"".decode()                     #string
data = b"".decode('utf-8')              #string
data = b"".decode(encoding='utf-8')     #string
data = str(b"")                         #string

Teste

O resultado deverá ser 'True'.
str1 = b'hello world'
str2 = bytes("hello world", encoding="UTF-8")
print(str1 == str2)

Comentários