#----string to bytes------ # 方法一:直接复制bytes类型 b'<str>' b = b'Hello World' print(type(b)) print(b) # 方法二:转换 s = 'Hello World' b = bytes(s,encoding='utf-8') print(type(b)) print(b) #----bytes to string------ s = str(b,encoding='utf-8') print(type(s)) print(s) #----执行结果------ <class 'bytes'> b'Hello World' <class 'bytes'> b'Hello World' <class 'str'> Hello World