python2与python3编码

fanhuasijin 2019-12-09

#coding:utf8#一#1.在python2中,默认以ASCII编码chcp 936import sysprint sys.getdefaultencoding()# ascii#str:bytess1=‘来星hello‘  #存的是字节,数据类型是str(bytes就是str)# print len(s1)# 9# print repr(s1) # ‘\xe8\xa2\x81\x16\xb5\x5ahello‘#2.unicodes2=u‘来童星hello‘# 存的unicodeprint repr(s2) # ‘\u8881\u8204ahello‘print type(s2)#‘unicode‘python2特点:print ‘hello‘+u‘star‘# hellostar ---->py2中将bytes转化为unicodeunicode看到的是明文#二 .在python3中,默认以UTF-8编码#在py3中严格区分bytes和strprint(b‘hello‘+‘star‘) #  TypeError: can‘t concat bytes to str存的时候文件的编码方式和解释器格式必须一致import sysprint(sys.getdefaultencoding())# utf-8

相关推荐