Differences between print in Python2 and Python3

Source: Internet
Author: User
The print () method is provided in both Python2 and Python3 to print information, but the differences between the two versions are mainly reflected in the following aspects: in 1python3, print is a built-in function that provides the print () method in both Python2 and Python3 to print information. However, the print between the two versions is slightly different.

It is mainly reflected in the following aspects:

1. print in python3 is a built-in function with multiple parameters, while print in python2 is a syntax structure;

2. you can print 'Hello world' with no brackets during Python2 printing, and print ("hello world") with brackets in Python3 ")

3. in Python2, the input string must be enclosed by quotation marks. to avoid reading non-string-type behaviors, raw_input () must be used instead of input ()

1. in python3, developers may feel that print has two identities at the same time, so they only keep the identity of the function:
>>> print 'pythontab.com'SyntaxError: Missing parentheses in call to 'print'

Therefore, print in python3 must use parentheses because it is a function.

2. the print function in python3 has multiple parameters. the Function prototype is as follows:
print(value1, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

As shown in the preceding method prototype,

1. print can support multiple parameters and print multiple strings at the same time (where... represents any number of strings );

2. sep indicates the character connection used between multiple strings;

3. end indicates the characters added to the end of the string. This parameter allows you to easily set print without line breaks. print statements in Python2.x will wrap the lines by default after the output string. if you do not want line breaks, you only need to add "," at the end of the statement. But in Python 3. x, print () becomes a built-in function, and the old method of adding "," won't work.

>>> Print ("python", "tab ",". com ", sep ='') pythontab.com >>> print ("python", "tab ",". com ", sep ='', end = '') # it can be printed without wrapping pythontab.com.
3. input pitfall in Python2
print ("what do you like")a = input("Enter any content:")print ("i like",a)

An error is reported when the string is input, which is well solved in python3.

The above is a detailed explanation of the differences between print in Python2 and Python3. For more information, see other related articles in the first PHP community!

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.