In this Python tutorial, we will discuss everything about invalid syntax Python. We will see, when “syntaxerror: invalid syntax” error appears in Python. And also how to fix invalid syntax error in Python.
Why invalid syntax error in Python?
An invalid syntax error in Python can appear for a multitude of reasons, some of which are:
- Misspelled Keywords: Python has a set of reserved keywords that should be spelled correctly. For instance, typing ‘fro’ instead of ‘for’ will result in a syntax error.
- Improper Indentation: Python uses indentation to differentiate between blocks of code. If your code is not indented correctly, Python will throw an error.
- Incorrect Punctuation: Each statement in Python must end with a newline. If a statement is broken up incorrectly or punctuation such as colons and parentheses are misplaced, it can result in a syntax error.
- Variable Names: If a variable name starts with a number or contains spaces or special characters (with the exception of underscores), it’s going to be a problem for Python.
- Incorrect Function Calls: If functions are called without the correct number or type of arguments, Python will throw an error.
How to fix invalid syntax in Python
Here are a few examples, that we can follow to fix invalid syntax in Python.
Example 1: Misspelled Keywords
Incorrect:
fro i in range(10):
print(i)
In the code above, ‘for’ is misspelled as ‘fro’, which results in a SyntaxError. Check out the output below:
Corrected:
for i in range(10):
print(i)
Simply correcting the spelling of ‘for’ fixes the issue.
Example 2: Improper Indentation
Incorrect:
def foo():
print("Hello, World!")
Python uses indentation to indicate blocks of code. In the code above, the ‘print’ statement isn’t indented correctly and hence Python raises a SyntaxError.
Corrected:
def foo():
print("Hello, World!")
By indenting the ‘print’ statement correctly under the function definition, the syntax error is resolved.
Example 3: Incorrect Punctuation
Incorrect:
if x == 10
print("x is 10")
The ‘if’ statement is missing a colon at the end which causes a SyntaxError. You can see the output when you run the code like below:
Corrected:
if x == 10:
print("x is 10")
Adding the missing colon resolves the syntax error.
Example 4: Incorrect Variable Names
Incorrect:
1var = "Invalid"
Variable names in Python cannot begin with a number. The above line will therefore raise a SyntaxError.
Corrected:
var1 = "Valid"
By changing the variable name to start with a character, the syntax error is resolved.
Example 5: Incorrect Function Calls
Incorrect:
print("Hello, World!", end = " ", "Goodbye, World!")
The print function does not accept multiple string arguments after a keyword argument. The above code will therefore raise a SyntaxError.
Corrected:
print("Hello, World!", end = " ")
print("Goodbye, World!")
Splitting the print statements into two corrects the issue.
Remember, the key to fixing syntax errors is carefully reading the error messages and understanding what Python is struggling to understand. With practice, you’ll find these become easier to spot and correct. I hope you can now fix the invalid syntax error in Python.
You may also like:
- indexerror: string index out of range in Python
- SyntaxError: Unexpected EOF while parsing
- ValueError: Invalid literal for int() with base 10 in Python
- syntaxerror invalid character in identifier python3
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.