python2与python3编码

#coding:utf8
#一
#1.在python2中,默认以ASCII编码
chcp 936
import sys
print sys.getdefaultencoding()# ascii
#str:bytes
s1='来星hello' #存的是字节,数据类型是str(bytes就是str)
# print len(s1)# 9
# print repr(s1) # '\xe8\xa2\x81\x16\xb5\x5ahello'
#2.unicode
s2=u'来童星hello'# 存的unicode
print repr(s2) # '\u8881\u8204ahello'
print type(s2)#'unicode'
python2特点:
print 'hello'+u'star'# hellostar ---->py2中将bytes转化为unicode
unicode看到的是明文
#二 .在python3中,默认以UTF-8编码
#在py3中严格区分bytes和str
print(b'hello'+'star') # TypeError: can't concat bytes to str
存的时候文件的编码方式和解释器格式必须一致
import sys
print(sys.getdefaultencoding())# utf-8
posted @ 2019-12-09 18:23  Stary_tx  阅读(417)  评论(0编辑  收藏  举报