字符串经由过程编码转换成字节码,字节码经由过程解码成为字符串
encode:str --> bytes(引荐进修:Python视频教程)
decode:bytes --> str
实例python 3.0+
str = "我是Python3" str_utf8 = str.encode('utf-8') str_gbk = str.encode('GBK') print(str) print("UTF-8 编码:", str_utf8) print("GBK 编码:",str_gbk) print("UTF-8 解码:", str_utf8.decode('utf-8')) print("GBK解码:",str_gbk.decode('GBK'))
输出效果以下:
我是Python3 UTF-8 编码: b'\xe6\x88\x91\xe6\x98\xafPython3'GBK 编码: b'\xce\xd2\xca\xc7Python3'UTF-8 解码: 我是Python3 GBK解码: 我是Python3
python3默许的编码为unicode,utf-8能够看作是unicode的一个扩大集
encode:指明要运用的编码,decode:指明当前编码的编码花样
更多Python相干技术文章,请接见Python教程栏目举行进修!
以上就是python3默许运用什么编码的细致内容,更多请关注ki4网别的相干文章!