前言
大家早好、午好、晚好吖 ❤ ~欢迎光临本文章
Python 3 最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。
文本总是 Unicode,由 str 类型表示,二进制数据则由 bytes 类型表示。
Python 3 不会以任意隐式的方式混用 str 和 bytes,正是这使得两者的区分特别清晰。
你不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然),也不能将字符串传入参数为字节包的函数(反之亦然).
python3.0 中怎么创建 bytes 型数据
>>> bytes([1,2,3,4,5,6,7,8,9])
>>> bytes("python", 'ascii') # 字符串,编码
字符串
'''
python资料获取看这里噢!! 小编 V:python10010 好友验证备注:6
即可获取文章源码/教程/资料/解答等福利,还有不错的视频学习教程和PDF电子书!
'''
>>> website = 'http://www.baidu.com/'
>>> type(website)
<class 'str'>
>>> website
'http://www.baidu.com/'
utf-8 转bytes
'''
python资料获取看这里噢!! 小编 V:python10010 好友验证备注:6
即可获取文章源码/教程/资料/解答等福利,还有不错的视频学习教程和PDF电子书!
'''
>>> website_bytes_utf8 = website.encode(encoding="utf-8")
>>> type(website_bytes_utf8)
<class 'bytes'>
>>> website_bytes_utf8
b'http://www.baidu.com/'
解码成 string,默认不填
>>> website_string = website_bytes_utf8.decode()
>>> type(website_string)
<class 'str'>
>>> website_string
'http://www.baidu.com/'
尾语
好了,今天的分享就差不多到这里了!
对下一篇大家想看什么,可在评论区留言哦!看到我会更新哒(ง •_•)ง
喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!