码迷,mamicode.com
首页 > 编程语言 > 详细

python2与python3编码

时间:2019-12-09 18:59:20      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:encoding   ror   code   tar   print   默认   必须   cat   特点   

#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

python2与python3编码

标签:encoding   ror   code   tar   print   默认   必须   cat   特点   

原文地址:https://www.cnblogs.com/startl/p/12012704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!