SyntaxError in Python: How to Handle Invalid Syntax in Python

Syntax errors are the single most common error encountered in programming.

Regardless of the language used, programming experience, or the amount of coffee consumed, all programmers have encountered syntax errors many times.

Syntax problems manifest themselves in Python through the SyntaxError exception. In this tutorial, I will teach you how to handle SyntaxError in Python, including numerous strategies for handling invalid syntax in Python.

What is a SyntaxError?

Syntax is the arrangement of words and phrases to create valid sentences in a programming language. A syntax error, in general, is any violation of the syntax rules for a given programming language.

With any human language, there are grammatical rules that we all must follow to convey meaning with our words. This is linguistic equivalent of syntax for spoken languages.

Programming languages attempt to simulate human languages in their ability to convey meaning. This means they must have syntax of their own to be functional and readable.

Syntax errors occur when a programmer breaks the grammatic and structural rules of the language. Syntax errors exist in all programming languages and differ based on the language's rules and structure.

In compiled languages such as C or Java, it is during the compilation step where SyntaxErrors are caught and raised to the developer. This is a compiler error as opposed to a runtime error.

According to Python's official documentation, a SyntaxError Exception is:

exception SyntaxError Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions exec() or eval(), or when reading the initial script or standard input (also interactively).

This is a very general definition and does not help us much in avoiding or fixing a syntax error. It's important to understand that these errors can occur anywhere in the Python code you write.

To be more specific, a SyntaxError can happen when the Python interpreter does not understand what the programmer has asked it to do.

Here is a simple example of a common syntax error encountered by python programmers.

def my_add_func(int a, int b):
    return a + b

This code will raise a SyntaxError because Python does not understand what the program is asking for within the brackets of the function. This is because the programming included the int keywords when they were not actually necessary. In Python, there is no need to define variable types since it is a dynamically typed language.

Because of this, the interpreter would raise the following error:

File "<stdin>", line 1
def add(int a, int b):
SyntaxError: invalid syntax

When a SyntaxError like this one is encountered, the program will end abruptly because it is not able to logically determine what the next execution should be. The programmer must make changes to the syntax of their code and rerun the program.

The Most Common SyntaxError in Python

The following code demonstrates what might well be the most common syntax error ever:

# Define a dict of Game of Thrones Characters

stark_ages = {
	'Bran': 10
	'Arya': 11,
	'Sansa': 13,
	'Jon': 16,
	'Robb': 16

Can you find the error?

The missing punctuation error is likely the most common syntax mistake made by any developer.

The infamous "Missing Semicolon" in languages like C, Java, and C++ has become a meme-able mistake that all programmers can relate to. This is such a simple mistake to make and does not only apply to those elusive semicolons.

In the case of our last code block, we are missing a comma , on the first line of the dict definition which will raise the following:

File "", line 6
	'Arya': 11,
SyntaxError: invalid syntax

After looking at this error message, you might notice that there is no problem with that line of the dict definition! You are absolutely right. The error is not with the second line of the definition, it is with the first line.

Error messages often refer to the line that follows the actual error. This causes some confusion with beginner Python developers and can be a huge pain for debugging if you aren't already aware of this.

The reason this happens is that the Python interpreter is giving the code the benefit of the doubt for as long as possible.

When defining a dict there is no need to place a comma on the last item: 'Robb': 16 is perfectly valid.

So, when the interpreter is reading this code, line by line, 'Bran': 10 could very well be perfectly valid IF this is the final item being defined in the dict.

The interpreter gives you the benefit of the doubt for this line of code, but once another item is requested for this dict the interpreter suddenly realizes there is an issue with this syntax and raises the error.

Indentation Errors

Python is unique in that it uses indendation as a scoping mechanism for the code, which can also introduce syntax errors.

Because of this, indentation levels are extremely important in Python. To see this in action, consider the following code block:

# Inconsistent Indentation
def foo():
    a = "A Lannister always pays his debts"
   b = "The things I do for love"
 c = "## Everyone who isn’t us, is an enemy"

Since this code block does not follow consistent indenting, it will raise a SyntaxError.

Normally indentation is 2 or 4 spaces (or a single tab - that is a hotly-debated topic and I am not going to get into that argument in this tutorial). This is very strictly controlled by the Python interpreter and is important to get used to if you're going to be writing a lot of Python code.

Python3's Print Function

Another extremely common syntax mistake made by python programming is the misuse of the print() function in Python3.

If you have recently switched over from Python v2 to Python3 you will know the pain of this error:

>>> a = "Hello World"
>>> print a
File "<stdin>", line 1
	print a
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(a)?

In Python version 2, you have the power to call the print function without using any parentheses to define what you want the print. Python3 removed this functionality in favor of the explicit function arguments list.

This error is so common and such a simple mistake, every time I encounter it I cringe!

Misspelling Keywords

Another very common syntax error among developers is the simple misspelling of a keyword.

Keywords are reserved words used by the interpreter to add logic to the code. For example: for, while, range, break, continue are each examples of keywords in Python.

Let's take a look at an example of a mispelled keyword in Python:

>>> fro i in range(0,3):
File "<stdin>", line 1
	fro i in range(0,3):
SyntaxError: invalid syntax

This mistake is very common because it can be caused by a slip of the finger when coding quickly. Maybe you've had a bit too much coffee? Or not enough? In any case, these errors are often fairly easy to recognize, which makes then relatively benign in comparison to more complex bugs.

Misuse of Keywords

Similarly, you may encounter a SyntaxError when using a Python keyword incorrectly.

>>> if 'Sansa' in stark_ages:
...     print("Sansa is a Stark!")
... 	break
File "<stdin>", line 3
SyntaxError: 'break' outside loop

The break keyword can only serve one purpose in Python: terminating a loop. That being the case, there isn't ever going to be used for the break keyword not inside a loop.

If you attempt to use break outside of a loop, you are trying to go against the use of this keyword and therefore directly going against the syntax of the language. This raises a SyntaxError.

Missing a Closing Symbol

Maybe symbols - such as {, [, ', and " - are designed to be paired with a closing symbol in Python. Neglecting to include a closing symbol will raise a SyntaxError.

Let's consider an example:

>>> got_quote = "Winter is coming  
File "<stdin>", line 1
    got_quote = "Winter is coming
SyntaxError: EOL while scanning string literal

This error is raised because of the missing closing quote at the end of the string literal definition.

As implied earlier, this same error is raised when dealing with parenthses:

>>> got_quote = "First lesson: Stick ‘em with the pointy end"
>>> print(got_quote
	File "<stdin>", line 1
SyntaxError: invalid syntax

This can be widely avoided when using an IDE which usually adds the closing quotes, parentheses, and brackets for you.

Final Thoughts

In summary, SyntaxError Exceptions are raised by the Python interpreter when it does not understand what operations you are asking it to perform.

The SyntaxError exception is most commonly caused by spelling errors, missing punctuation or structural problems in your code. These are the grammatical errors we find within all languages and often times are very easy to fix.

For the most part, these are simple mistakes made while writing the code. For the most part, they can be easily fixed by reviewing the feedback provided by the interpreter.

If you enjoyed this article, be sure to join my Developer Monthly newsletter, where I send out the latest news from the world of Python and JavaScript:

Written on August 1st, 2020