一、print
在 python2 中 print 是一条语句,而在 python3 中 print 作为函数存在。
# python2 >>> print ("hello") hello # python3 >>> print ("hello") hello
这样看好像没有什么区别,python2 好像也可以把 print 当做函数使用,但是仅仅是表象而已,前者是把 ("hello")当作一个整体,而后者 print()是个函数,接收字符串作为参数。
#python2 >>> print ("hello","world") ('hello', 'world') #python3 >>> print ("hello","world") hello world
这样看就非常明显了,python2 中,print 语句后接的是元组对象 ("hello","world")。
而 python3 中,print() 函数是得到了两个位置参数 hello 和 world 。
如果想在 python2 中把 print 当做函数使用,可以导入 __future__ 模块中的 print_function
>>> print ("Hello","world") ('Hello', 'world') >>> >>> from __future__ import print_function >>> print ("Hello","world") Hello world
二、编码
python2 默认编码是 asscii,所以不能直接打印中文,如果要在脚本中使用中文,需要在脚本中声明使用utf-8,# -*- coding: utf-8 -*-,
#!/usr/bin/env python # -*- coding: utf-8 -*- print "您好"
而在 python3 中,默认编码已经是 utf-8 了,所以已经不需要单独声明了,可以直接打印中文内容。
#!/usr/bin/env python print ("您好")
三、用户输入
python 2 用户输入使用 raw_input(),python 3 使用 input(),python2 中也可以使用 input(),但是参数只能是变量,不推荐在python 2 中使用 input()。
#!/usr/bin/env python #user_input = raw_input("Please input something: ") #only on python 2.x user_input = input("Please input something: ") print(user_input)