您的位置:首页 > 编程语言 > Python开发

Python之路Python3【第零篇】Python2 & Python3区别持续更新~

2016-05-13 22:18 489 查看

print

def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
pass


Python2中print是一个语句,只需要向吧输出的放在print关键字后面就可以输出。

Python3中从上面的代码中可以看出他现在是一个函数,就像其他函数一样,print()需要您将要输出的内容作为参数传给他。

Unicode字符串

Python 2有两种字符串类型:Unicode字符串和非Unicode字符串。Python 3只有一种类型:Unicode字符串(Unicode strings)。

1、Python2里的Unicode字符串在Python3里也是普通的字符串,因为在Python里字符串总是Unicode形式的

所以你就可以用下面的方式命名变量了,但是不要这样使用~~

帅哥 = '罗天帅'
print(帅哥)


2、Unicode原始字符串(raw string),使用这种字符串,Python不会自动转移反斜线“\”也会被替换为普通的字符串,因为在Python3里所有的原始字符串也都是Unicode编码的

Python 2有两种字符串类型:Unicode字符串和非Unicode字符串。Python 3只有一种类型:Unicode字符串(Unicode strings)

3、Python 2有两个全局函数可以把对象强制转换成字符串:
unicode()
把对象转换成Unicode字符串,还有
str()
把对象转换为非Unicode字符串。Python 3只有一种字符串类型,Unicode字符串,所以
str()
函数即可完成所有的功能。(
unicode()
函数在Python 3里不再存在了。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: