热线电话:13121318867

登录
2019-03-15 阅读量: 650
python2与python3有什么区别与联系?(一)

print 函数

print语句没有了,取而代之的是print()函数。 Python 2.6与Python 2.7部分地支持这种形式的print语法。在Python 2.6与Python 2.7里面,以下三种形式是等价的:

print 'hello world!'
print ("hello world!") #注意print后面有个空格
print("hello world!") #print()不能带有任何其它参数

然而,Python 2.6实际已经支持新的print()语法:


from __future__ import print_function
print("fish", "panda", sep=', ')

Unicode

Python 2 有 ASCII str() 类型,unicode() 是单独的,不是 byte 类型。

现在, 在 Python 3,我们最终有了 Unicode (utf-8) 字符串,以及一个字节类:byte 和 bytearrays。

由于 Python3.X 源码文件默认使用utf-8编码,这就使得以下代码是合法的:

>>> 中国 = 'china' 
>>>print(中国)
china

Python 2.x:

>>> str = "我爱北京天安门"
>>> str
'\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8'
>>> str = u"我爱北京天安门"
>>> str
u'\u6211\u7231\u5317\u4eac\u5929\u5b89\u95e8'

Python 3.x:

>>> str = "我爱北京天安门"
>>> str
'我爱北京天安门'
3.2276
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子