![图片[1]-python中str和bytes如何相互转化?-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/1613783004173313.png)
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
string转为bytes
方法一:使用utf-8 的方式编码,转成 bytes
>>>website_bytes_utf8=website.encode(encoding="utf-8") >>>type(website_bytes_utf8) <class'bytes'> >>>website_bytes_utf8 b'http://www.jb51.net/'
方法二:使用编码encode,转化成bytes
str="aabbcc"
bytes=str.encode('utf-8')
print(str)
aabbcc
print(bytes)
b'aabbcc'
bytes转为string
方法一:使用utf-8 的方式编码,转成string
>>>website_string=website_bytes_utf8.decode() >>>type(website_string) <class'str'> >>>website_string 'http://www.jb51.net/'
方法二:使用解码decode,转化成string
bytes=b"aabbcc"
str=bytes.decode('utf-8')
print(bytes)
b'aabbcc'
print(str)
aabbcc原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容