其他分享
首页 > 其他分享> > 第7章习题

第7章习题

作者:互联网

1

创建一个Unicode字符串mystery并将它的值设为'\U0001f4a9'。打印mystery,并查看mystery的Unicode名称
import unicodedata
mystery = '\U0001f4a9'
print(mystery)
unicodedata.name(mystery)

要点:
python3 中的字符串都是以Unicode进行编码的。

2

使用UTF-8对mystery进行编码,存入字节型变量pop_bytes,并把它打印出来。
mystery = '\U0001f4a9'
pop_bytes = mystery.encode('utf-8')
print(pop_bytes)

输出

b'\xf0\x9f\x92\xa9'

要点:
编码后为字节型变量

3

# 使用UTF-8对pop_bytes进行解码,存入字符串型变量pop_string,并将它打印出来。
pop_byte = '\U0001f4a9'.encode('utf-8')
pop_string = pop_byte.decode('utf-8')
print(pop_string)

要点
utf-8是Unicode的压缩格式,将Unicode编码成utf-8便于传输和储存,

标签:mystery,utf,bytes,U0001f4a9,pop,Unicode,习题
来源: https://blog.csdn.net/Hollybobo79/article/details/83929304