[Python]Bytes 和 String转换

#----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

 

posted @ 2020-02-12 20:00  LeoShi2020  阅读(576)  评论(0编辑  收藏  举报