您好,欢迎来到要发发知识网。
搜索
您的当前位置:首页中文在python中占几个字节

中文在python中占几个字节

来源:要发发知识网

如果是utf-8编码,那么一个中文字符占用三个字节,一个英文字符占用一个字节。如果是gbk编码,那么一个中文字符占用两个字节,一个英文字符占用一个字节。

如果是utf-8编码,那么一个中文包含繁体字等于三个字节,一个英文字符等于一个字节。

如果是gbk编码,那么一个中文包含繁体字等于两个字节,一个英文字符等于一个字节。

(推荐学习:)

我们可以用如下方法来判断:

中文和符号:

print(type('中文'.encode('utf-8')))  #输出为bytes类型
执行结果:
<class 'bytes'>

print(type('中文'.encode('gbk')))
执行结果:
<class 'bytes'>
print(len('中文'.encode('utf-8')))  #输出几个字节
执行结果:
6

print(len('中文'.encode('gbk')))
执行结果:
4

print(len(',。'.encode('gbk')))
执行结果:
4

英文字母和符号:

print(type('ab'.encode('utf-8')))
执行结果:
<class 'bytes'>
print(len('ab'.encode('utf-8')))
执行结果:
2

print(len(',.'.encode('gbk')))
执行结果:
2

Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务