Home >Backend Development >Python Tutorial >Why Does `print 'hello World'` Cause a Syntax Error in Python 3?

Why Does `print 'hello World'` Cause a Syntax Error in Python 3?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-11 17:41:10898browse

Why Does `print

Syntax Error in Print Statement with Python 3

When trying to print a string in Python 3 using the syntax print "hello World", users may encounter a syntax error. This error is due to a change in the print statement in Python 3.

In Python 2, print was a statement that did not require parentheses. However, in Python 3, print evolved into a function, and parentheses are now required when calling it. The correct syntax in Python 3 is print("hello World").

This change was made to enhance clarity and consistency in the language. Functions in Python are typically enclosed in parentheses to indicate that they are being called. By making print a function, Python 3 ensures that it follows the same convention as other functions.

Therefore, to resolve the syntax error when printing a string in Python 3, users should surround the string with parentheses, as seen in the corrected example above.

The above is the detailed content of Why Does `print 'hello World'` Cause a Syntax Error in Python 3?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn