Proxy locations

Europe

North America

South America

Asia

Africa

Oceania

See all locations

Network statusCareers

Back to blog

Python Syntax Errors: Common Mistakes and How to Fix Them

Python Syntax Errors: Common Mistakes and How to Fix Them

Vytenis Kaubrė

2023-05-089 min read
Share

Whether you’re a true Pythonista or you’ve just dipped your toes into Python programming, you’ve probably had your battles with syntax errors. It takes a mere missing punctuation mark for the code to crash, and sometimes the error message doesn’t even give enough clues.

Read this article to learn how to understand, avoid, and fix Python syntax errors with the help of practical examples.

What are syntax errors in Python?

Syntax errors are invalidities in the code's structure. In other words, Python syntax errors happen when the Python interpreter is unable to recognize the structure of statements in code.

Every language follows certain rules to make sense of what’s being told and written. We can’t omit certain words and letters, as that can change the meaning entirely. Programming languages, including Python, are no exception. Your code must follow Python’s syntax rules for it to make sense. 

Any programming language acts as a translator of human language to machine language. Hence, there are specific steps to code execution. When you run the code, the Python interpreter first parses your code into smaller components, then translates them into byte code, and eventually executes it.

During the parsing process, the interpreter analyzes your code for invalid syntax, and when it finds the first instance of incorrect syntax use, it gives you that unwelcome SyntaxError message.

How to read Python syntax errors

When you get an error message, Python tries to point to the root cause of the error. Sometimes, the message tells exactly what’s the problem, but other times it’s unclear and even confusing. This happens because Python locates the first place where it couldn’t understand the syntax; therefore, it might show an error in a code line that goes after the actual error.

Knowing how to read Python error messages goes a long way to save both time and effort. So let’s examine a Python web scraping code sample that raises two syntax errors:

prices = {"price1": 9.99, "price2": 13.48 "price3": 10.99, "price4": 15.01}
price_found = False
for key value in prices.items():
    if 10 <= value <= 14.99:
        print(key + ":", value)
        price_found = True
if not price_found:
    print("There are no prices between $10 and $14.99")
Link to GitHub

In this example, we have a dictionary of different prices. We use a for loop to find and print the prices between $10 and $14.99. The price_found variable uses a boolean value to determine whether such a price was found in the dictionary.

When executed, Python points to the first invalid syntax error it came upon, even though there are two more errors along the way. The first error message looks like this:

Python SyntaxError message

Information in the yellow box helps us determine the location of the error, and the green box includes more details about the error itself. The full message can be separated into four main elements:

  1. The path directory and name of the file where the error occurred;

  2. The line number and the faulty code line where the error was first encountered;

  3. The carets (^) that pinpoint the place of the error;

  4. The error message determines the error type, followed by additional information that may help fix the problem.

The code sample produced a syntax error found in the first line of code – the prices dictionary. The carets indicate that the error occurred between “price2”: 13.48 and “price3”: 10.99, and the invalid syntax message says that perhaps we forgot to add a comma between the items in our dictionary. That’s exactly it! The Python interpreter suggested the correct solution, so let’s update the code:

prices = {"price1": 9.99, "price2": 13.48, "price3": 10.99, "price4": 15.01}
Link to GitHub

Now, rerun the code to see what’s the second syntax error:

Python SyntaxError message

This time, the carets fail to pinpoint the exact location of the error, and the SyntaxError message doesn’t include additional information about the possible solution. In such cases, the rule of thumb would be to examine the code that comes just before the carets. In the code sample, the syntax error is raised because there’s a missing comma between the variables key and value in the for loop. The syntactically correct code line should look like this:

for key, value in prices.items():
Link to GitHub

When it comes to the causes of Python syntax errors, sometimes there’s just not enough information to get you on the right track. Case in point the above example of an invalid syntax error. Thus, it helps to know some of the common reasons that bring about syntax errors in Python to resolve them easily.

Common causes of syntax errors

Generally, you can expect common Python syntax errors to be caused by these invalidities in code:

  • Misplaced, missing, or mismatched punctuation (parentheses, brackets, braces, quotes, commas, colons, etc.);

  • Misspelled, misplaced, or missing Python keywords;

  • Illegal characters in variable names;

  • Incorrect indentation;

  • Incorrect use of the assignment operator (=).

The lack of experience with Python, copy-pasting errors, and typographical errors play a big role here. Especially the latter, as typos are easy to make and hard to spot, so it’s a great practice to double-check your code during production.

In any case, there are additional steps you can take when coding to decrease the chances of making a syntax error.

How to avoid Python syntax errors?

  1. Use an integrated development environment (IDE) or a code editor that highlights the reserved Python keywords and errors, as well as suggests edits as you write your code. This is the first and foremost step you should take to improve your code and avoid invalid syntax errors in Python.

  2. Frequently test your code as you write it. This helps to find syntax errors from the get-go and minimizes the risk of repeating the same mistakes down the road.

  3. Use consistent indentation. Incorrect indentation is one of the more frequent causes of syntax errors in Python, which can be avoided by consciously checking your code and using IDEs or code editors that highlight indentation errors.

  4. Use linting and formatting modules that check your code for errors and style. Some modules can fix your code automatically, saving you time and effort. Linters and formatters are great when you only want to run Python in the Terminal without an IDE or a code editor. A few notable mentions include pycodestyle, pylint, pyflakes, Flake8, and Black.

  5. Read Python documentation. It’s indeed an obvious tip to give, but the official Python documentation offers lots of useful information that will not only deepen your knowledge but also help you understand Python’s syntax and the meaning of syntax error messages.

Remember that these steps can help you avoid common Python syntax errors but not eliminate them.

Even if your code is syntactically correct but something is off when the code runs – an exception occurs. Python runtime errors happen during code execution and change the normal flow of the program. Unlike syntax errors, which terminate the program once the error occurs, runtime errors can continue to run the program if proper exception handlers are used. 

Furthermore, logical errors happen when the code is syntactically valid and runs without errors, but the output isn't what was expected. While this article examines invalid syntax problems, it should be noted that the SyntaxError exception is part of a wider group of exceptions. For advanced Python programming, learning about other errors and how to handle them is recommended.

How to fix syntax errors

Now that we’ve discussed the common causes of Python syntax errors and the general tips on avoiding them let’s see some actual code samples and fix them.

Misplaced, missing, or mismatched punctuation

1. Ensure that parentheses (), brackets [], and braces {} are properly closed. When left unclosed, the Python interpreter treats everything following the first parenthesis, bracket, or brace as a single statement. Take a look at this web scraping code sample that sends a set of crawling instructions to our Web Crawler tool:

payload = {
    "url": "https://www.example.com/",
    "filters": {
        "crawl": [".*"],
        "process": [".*"],
        "max_depth": 1,
    "scrape_params": {
        "user_agent_type": "desktop",
    },
    "output": {
        "type_": "sitemap"
    }
}

# Error message
  File "<stdin>", line 1
    payload = {
              ^
SyntaxError: '{' was never closed
Link to GitHub

At first glance, it looks like the payload was closed with braces, but the Python interpreter raises a syntax error that says otherwise. In this particular case, the “filters” parameter isn’t closed with braces, which the interpreter, unfortunately, doesn’t show in its traceback. You can fix the error by closing the “filters” parameter:

payload = {
    "url": "https://www.amazon.com/",
    "filters": {
        "crawl": [".*"],
        "process": [".*"],
        "max_depth": 1
    }, # Add the missing brace
    "scrape_params": {
        "user_agent_type": "desktop",
    },
    "output": {
        "type_": "sitemap"
    }
}
Link to GitHub

2. Make sure you close a string with proper quotes. For example, if you started your string with a single quote ‘, then use a single quote again at the end of your string. The below code snippet illustrates this:

list_of_URLs = (
    'https://example.com/1',
    'https://example.com/2",
    'https://example.com/3
)
print(list_of_URLs)

# Error message
  File "<stdin>", line 3
    'https://example.com/2",
    ^
SyntaxError: unterminated string literal (detected at line 3)
Link to GitHub

This example has two errors, but as you can see, the interpreter shows only the first syntax error. It pinpoints the issue precisely, which is the use of a single quote at the start, and a double quote at the end to close the string. 

The second error is in the third example URL, which isn’t closed with a quotation mark at all. The syntactically correct version would look like this:

list_of_URLs = (
    'https://example.com/1',
    'https://example.com/2',
    'https://example.com/3'
)
print(list_of_URLs)
Link to GitHub

When the string content itself contains quotation marks, use single , double , and/or triple ‘’’ quotes to specify where the string starts and ends. For instance:

print("In this example, there's a "quote within 'a quote'", which we separate with double and single quotes.")

# Error message
  File "<stdin>", line 1
    print("In this example, there's a "quote within 'a quote'", which we separate with double and single quotes.")
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
Link to GitHub

The interpreter shows where the error occurred, and you can see that the carets end within the second double quotation mark. To fix the syntax error, you can wrap the whole string in triple quotes (either ’’’ or “””):

print("""In this example, there's a "quote within 'a quote'", which we specify with double and single quotes.""")
Link to GitHub

3. When passing multiple arguments or values, make sure to separate them with commas. Consider the following web scraping example that encapsulates HTTP headers in the headers dictionary:

headers = {
    'Accept': 'text/html',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US, en;q=0.9'
    'Connection': 'keep-alive'
}

# Error message
  File "<stdin>", line 5
    'Connection': 'keep-alive'
                ^
SyntaxError: invalid syntax
Link to GitHub

Again, the interpreter fails to show precisely where the issue is, but as a rule of thumb, you can expect the actual invalid syntax error to be before where the caret points. You can fix the error by adding the missing comma after the ‘Accept-Language’ argument:

headers = {
    'Accept': 'text/html',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US, en;q=0.9',
    'Connection': 'keep-alive'
}
Link to GitHub

4. Don’t forget to add a colon : at the end of a function or a compound statement, like if, for, while, def, etc. Let’s see an example of web scraping:

def extract_product_data()
    for url in product_urls
        response = requests.get(url, headers=headers)
        soup = BeautifulSoup(response.content, 'html.parser')
        title = soup.find("h1").text
        price = soup.find("span", {"itemprop": "price"}).text
        product_data.append({
            "title": title,
            "price": price,
        })

# Error message
File "<stdin>", line 1
    def extract_product_data()
                              ^
SyntaxError: expected ':'
Link to GitHub

This time, the interpreter shows the exact place where the error occurred and hints as to what could be done to fix the issue. In the above example, the def function and the for loop are missing a colon, so we can update our code:

def extract_product_data():
    for url in product_urls:
Link to GitHub

Misspelled, misplaced, or missing Python keywords

1. Make sure you’re not using the reserved Python keywords to name variables and functions. If you’re unsure whether a word is or isn’t a Python keyword, check it with the keyword module in Python or look it up in the reserved keywords list. Many IDEs, like PyCharm and VS Code, highlight the reserved keywords, which is extremely helpful. The code snippet below uses the reserved keyword pass to hold the password value, which causes the syntax error message:

user = 'username1'
pass = 'password1'

# Error message
  File "<stdin>", line 2
    pass = 'password1'
         ^
SyntaxError: invalid syntax
Link to GitHub

2. Ensure that you haven’t misspelled a Python keyword. For instance:

import time
from requests impotr Session

# Error message
  File "<stdin>", line 2
    from requests impotr Session
                  ^^^^^^
SyntaxError: invalid syntax
Link to GitHub

This code sample tries to import the Session object from the requests library. However, the Python keyword import is misspelled as impotr, which raises an invalid syntax error.

3. Placing a Python keyword where it shouldn't be will also raise an error. Make sure that the Python keyword is used in the correct syntactical order and follows the rules specific to that keyword. Consider the following example:

import time
import Session from requests

# Error message
  File "<stdin>", line 2
    import Session from requests
                   ^^^^
SyntaxError: invalid syntax
Link to GitHub

Here, we see an invalid syntax error because the Python keyword from doesn’t follow the correct syntactical order. The fixed code should look like this:

import time
from requests import Session
Link to GitHub

Illegal characters in variable names

Python variables have to follow certain naming conventions:

1. You can’t use blank spaces in variable names. The best solution is to use the underscore character. For example, if you want a variable named “two words”, it should be written as two_words, twowords, TwoWords, twoWords, or Twowords.

2. Variables are case-sensitive, meaning example1 and Example1 are two different variables. Take this into account when creating variables and calling them later in your code.

3. Don’t start a variable with a number. Python will give you a syntax error:

response1 = requests.get(url)
2response = requests.post(url)

# Error message
  File "<stdin>", line 2
    2response = requests.post(url)
    ^
SyntaxError: invalid decimal literal
Link to GitHub

As you can see, the interpreter allows using numbers in variable names but not when the variable names start with a number.

4. Variable names can only use letters, numbers, and underscores. Any other characters used in the name will produce a syntax error.

Incorrect indentation

1. Remember that certain Python commands, like compound statements and functions, require indentation to define the scope of the command. So, ensure that such commands in your code are indented properly. For instance:

prices = (16.99, 13.68, 24.98, 14.99)


def print_price():
    for price in prices:
    if price < 15:
        print(price)

print_price()


# Error message 1
  File "<stdin>",line 6
    if price < 15:
    ^
IndentationError: expected an indented block after 'for' statement on line 5


# Error message 2
  File "<stdin>", line 7
    print(price)
    ^
IndentationError: expected an indented block after 'if' statement on line 6
Link to GitHub

The first error message indicates that the if statement requires an indented block. After fixing that and running the code, we encounter the second error message that tells us the print statement is outside the if statement and requires another indent. Fix the code with the correct indentation:

prices = (16.99, 13.68, 24.98, 14.99)


def print_price():
    for price in prices:
        if price < 15:
            print(price)

print_price()
Link to GitHub

2. Use consistent indentation marks: either all spaces or all tabs. Don’t mix them up, as it can reduce the readability of your code, in turn making it difficult to find the incorrect indentation just by looking at the code. Most Python IDEs highlight indentation errors before running the code, so you can reformat the file automatically to fix the indentation. 

Let’s take the above code sample and fix the first error message by adding a single space in front of the if statement:

prices = (16.99, 13.68, 24.98, 14.99)


def print_price():
    for price in prices:
     if price < 15:
        print(price)

print_price()

The code works without errors and prints the correct result. However, you can see how the mix of spaces and tabs makes the code a little harder to read. Using this method can bring about unnecessary syntax errors when they can be avoided by sticking to either spaces or tabs throughout the code.

Incorrect use of the assignment operator (=)

1. Ensure you aren’t assigning values to functions or literals with the assignment operator =. You can only assign values to variables. Here’s an overview of some examples:

"price" = 10.98

# Error message
  File "<stdin>", line 1
    "price" = 10.98
    ^^^^^^^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Link to GitHub

The string "price" can't act as a variable, therefore the quotation marks have to be removed:

price = 10.98
Link to GitHub

In the following example, we want to check whether the value 10.98 is a float type:

price = 10.98
type(price) = float

# Error message
  File "<stdin>", line 2
    type(price) = float
    ^^^^^^^^^^^
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Link to GitHub

The Python interpreter raises an error since the assignment operator can’t be used to assign a value to a function. The correct way to accomplish this is with one the following code samples:

price = 10.98
print(type(price))

# or

price = 10.98
is_float = type(price) == float
print(is_float)
Link to GitHub

2. Assign values in a dictionary with a colon : and not an assignment operator =. Let’s take a previous code sample and modify it to incorrectly use the assignment operator instead of colons:

headers = {
    'Accept' = 'text/html',
    'Accept-Encoding' = 'gzip, deflate, br',
    'Accept-Language' = 'en-US, en;q=0.9',
    'Connection' = 'keep-alive'
}


# Error message
  File "<stdin>", line 2
    'Accept' = 'text/html',
    ^^^^^^^^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Link to GitHub

3. Use == when comparing objects based on their values. For instance:

price_1 = 200.99
price_2 = 200.98

compare = (price_1 = price_2)
print(compare)

# Error message
  File "<stdin>", line 4
    compare = (price_1 = price_2)
               ^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
Link to GitHub

You can fix the issue by using the double equal sign == between price_1 and price_2 instead of =, which will print the correct result.

Final thoughts

Syntax errors in Python can be tough to crack, depending on the error. When you have a clear understanding of how error tracebacks work, along with the common reasons behind these errors and their possible solutions, you can significantly reduce the time and effort required to debug issues. Hopefully, this article helped you get on the right track to start producing syntactically flawless Python code.

You can find the source code files together with their respective fixes in our GitHub repository. In case you’re interested to learn more about web scraping with Python, check out our in-depth tutorial here.

If you have any questions about our products, our support team is always here to assist you 24/7. Reach out via email or live chat.

About the author

Vytenis Kaubrė

Junior Technical Copywriter

Vytenis Kaubrė is a Junior Technical Copywriter at Oxylabs. His love for creative writing and a growing interest in technology fuels his daily work, where he crafts technical content and web scrapers with Oxylabs’ solutions. Off duty, you might catch him working on personal projects, coding with Python, or jamming on his electric guitar.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Get the latest news from data gathering world

I'm interested

IN THIS ARTICLE:


  • What are syntax errors in Python?


  • Common causes of syntax errors


  • How to fix syntax errors


  • Final thoughts

Forget about complex web scraping processes

Choose Oxylabs' advanced web intelligence collection solutions to gather real-time public data hassle-free.

Scale up your business with Oxylabs®