码迷,mamicode.com
首页 > 编程语言 > 详细

[Python]Bytes 和 String转换

时间:2020-02-12 20:17:25      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:class   bytes   类型   odi   coding   pre   utf-8   byte   结果   

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

 

[Python]Bytes 和 String转换

标签:class   bytes   类型   odi   coding   pre   utf-8   byte   结果   

原文地址:https://www.cnblogs.com/leoshi/p/12300584.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!