Common print() Errors in Python and How to Fix Them
Missing Quotes
The most common print() error of all: forgetting the quotes around text.
print(Hello)NameError: name 'Hello' is not defined
Why it happens
Without quotes, Python doesn't see text. It sees a name, and it looks for something called Hello. Nothing has that name, so Python stops with a NameError: "I don't know this name".
You'll learn in the Variables chapter that names can hold values. That's exactly why Python looks for one here.
Two words without quotes
With more than one word, the message changes:
print(Hello World)SyntaxError: invalid syntax. Perhaps you forgot a comma?
Now Python sees two names side by side, with nothing between them, and it can't make sense of the line at all. That's a SyntaxError: the code breaks Python's grammar rules. Python even guesses what you meant ("Perhaps you forgot a comma?"), but here the real fix is quotes.
The fix
Put the text inside quotes, single or double:
print("Hello World")Hello World
Rule of thumb: if a NameError or SyntaxError points at a print() line, first check that every piece of text has quotes at both ends.
NameError on a print() line? Look for text without quotes.