There are two reasons:
bytes represent two data types,
stris a string type, and
bytesis a byte type.
encodestr to get bytes, and
decodebytes to get str.
The two are mutually converted. One of the reasons for the above problem is the use of decoding on the str string, which is obviously the pig's head and the horse's tail.
txt ='Hello, shiyi, thank you very much for staying with me.' #str->bytes encode txt = txt.encode('utf-8') print(type(txt)) #bytes->str decode txt = txt.decode('utf-8') print(type(txt)) # results <class'bytes'> <class'str'> Process finished with exit code 0
decode, you can only convert
encodeto bytes, and then
decode('utf-8')first encode into bytes, then Decode into string.