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) # 'xe8xa2x81x16xb5x5ahello'
#2.unicode
s2=u'来童星hello'# 存的unicode
print repr(s2) # 'u8881u8204ahello'
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
原文地址:https://www.cnblogs.com/startl/p/12012704.html