Home > Backend Development > Python Tutorial > Why Am I Getting a SyntaxError When Printing Strings in Python 3?

Why Am I Getting a SyntaxError When Printing Strings in Python 3?

Patricia Arquette
Release: 2024-12-17 20:15:10
Original
407 people have browsed it

Why Am I Getting a SyntaxError When Printing Strings in Python 3?

Syntax Error in Python 3: Printing Strings

When attempting to print a string using the print statement in Python 3, you might encounter a syntax error. The following error message indicates that the syntax of the print statement is incorrect:

SyntaxError: invalid syntax
Copy after login

Problem:

The specific syntax error occurs when the print statement is used in the following format:

print "hello World"
Copy after login

This syntax is not valid in Python 3.

Solution:

In Python 3, the print statement has been modified to become a function. This change requires you to include parentheses when using the print statement, as follows:

print("Hello World")
Copy after login

By making this modification, you ensure that the syntax of the print statement is correct and that the string will be printed to the console.

The above is the detailed content of Why Am I Getting a SyntaxError When Printing Strings in Python 3?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template