Reason for syntax error (syntaxerror:invalid syntax) using the Print function in Python 3.x

Source: Internet
Author: User
Tags lua

After installing the latest version of the Python 3.x version,

To refer to someone else's code (based on a Python 2.x writing tutorial), to use the print function, when the output is printed, the result is a syntax error with the PRINT function:

Syntaxerror:invalid syntax

This is because the syntax of the Python 2.x upgrade to the Python 3.x,print function is changed, so the code for Python 2.x's print function, which runs in Python 3.x, results in the print function "SyntaxError: Invalid syntax. "

Differences in the syntax of print functions in Python 2.x and Python 3.x

The most concise explanation is:

Python 2.x:print "What to print" without parentheses

Python 3.x:print function ("What to print"), must be enclosed in parentheses

For example, it is:

1. Formatted with no percent sign

Python 2.x:

print"Pyhon 2 can use print string without ()";

Python 3.x:

print("Python3, print must use () to output string");

2. Formatted with percent

Python 2.x:

print"old %s version is %d, print no ()"%("Python"2);

Python 3.x:

print("new %s version is %d, print must have ()"%("Python"3));

Reason for syntax error (syntaxerror:invalid syntax) using the Print function in Python 3.x

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.