Main differences between Python2 and 3: differences between Python2
I. print
Print is a statement in python2, and print exists as a function in python3.
# python2>>> print ("hello")hello# python3 >>> print ("hello")hello
In this case, there seems to be no difference. python2 can also use print as a function, but it is only a representation. The former regards ("hello") as a whole, while the latter regards print () is a function that receives strings as parameters.
#python2>>> print ("hello","world")('hello', 'world')#python3>>> print ("hello","world")hello world
In python2, the print statement is followed by a tuples ("hello", "world ").
In python3, the print () function obtains two location parameters hello and world.
If you want to use print as a function in python2, You can import print_function In the _ future _ module.
>>> print ("Hello","world")('Hello', 'world')>>> >>> from __future__ import print_function>>> print ("Hello","world")Hello world
Ii. Encoding
Python2 is encoded as asscii by default, so Chinese characters cannot be printed directly. To use Chinese characters in the script, declare UTF-8 in the script, #-*-coding: UTF-8 -*-,
#! /Usr/bin/env python #-*-coding: UTF-8-*-print "hello"
In python3, the default encoding is UTF-8, so you do not need to declare it separately. You can print Chinese content directly.
#! /Usr/bin/env pythonprint ("hello ")
Iii. user input
Python 2 user input uses raw_input (), python 3 uses input (), and python2 can also use input (), but the parameter can only be a variable, input () is not recommended in python 2 ().
#!/usr/bin/env python#user_input = raw_input("Please input something: ") #only on python 2.xuser_input = input("Please input something: ")print(user_input)