I am trying to use str.encode() but I get
str.encode()
>>> \"hello\".encode(hex) Traceback (most recent call last): File \"\", line 1
In Python 3.5+, encode the string to bytes and use the hex() method, returning a string.
hex()
s = "hello".encode("utf-8").hex() s # '68656c6c6f'
Optionally convert the string back to bytes:
b = bytes(s, "utf-8") b # b'68656c6c6f'