Main differences between Python2 and 3: differences between Python2

Source: Internet
Author: User

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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.