作业15:编码进阶

# encode 编码 str ---> bytes
s1 = 'alex'
s2 = s1.encode('utf-8')
s3 = s1.encode('gbk')
print(s2)
print(s3)

m1 = '中国'
m2 = m1.encode('utf-8')
m3 = m1.encode('gbk')
print(m2)
print(m3)
# py3:
# 文件的储存,传输,不能是unicode(只能是utf-8 utf-16 gbk,gb2312,asciid等)
#     str 在内存中是用unicode编码。 存储、传输时用bytes
#         bytes类型
#         对于英文:
#              str  :表现形式:s = 'alex'
#                     编码方式: 010101010  unicode
#             bytes :表现形式:s = b'alex'
#                     编码方式: 000101010  utf-8 gbk。。。。
#
#         对于中文:
#              str  :表现形式:s = '中国'
#                     编码方式: 010101010  unicode
#             bytes :表现形式:s = b'xe91e91e01e21e31e32'
#                     编码方式: 000101010  utf-8 gbk。。。。

  

原文地址:https://www.cnblogs.com/elsa2007/p/10941429.html