[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

猜你喜欢

转载自www.cnblogs.com/leoshi/p/12300584.html