SyntaxError: invalid syntax in if statement in Python [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# SyntaxError: invalid syntax in if statement in Python

The Python "SyntaxError: invalid syntax" is often caused when we use a single equals sign instead of double equals in an if statement.

To solve the error, use double equals == if comparing values and make sure the line of the if statement ends with a colon.

syntaxerror invalid syntax if statement

# Using a single equals sign when comparing

Here is an example of how the error occurs.

main.py
name = 'Bobby Hadz' # ⛔️ SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? if name = 'Bobby Hadz': print('success')

using single equals sign

The error is caused because we used a single equals sign instead of double equals.

# Use double equals when comparing values

If comparing values, make sure to use double equals.

main.py
name = 'Bobby Hadz' # ✅ Using double equals when comparing if name == 'Bobby Hadz': # 👇️ This runs print('success')

use double quotes when comparing values

Always use double equals (==) when comparing values and single equals (=) when assigning a value to a variable.

main.py
# ✅ Assignment single (=) equals name = 'bobby hadz' # ✅ Comparison double (==) equals if name == 'bobby hadz': print('success')

# Make sure the line of the if statement ends with a colon

Make sure the line of the if statement ends with a colon :.

main.py
name = 'bobby hadz' if name == 'bobby hadz': # 👈️ Must end with colon print('success')

make sure the line of the if statement ends with colon

If you forget the colon at the end of the line, the error occurs.

# Forgetting to indent your code properly

Another common cause of the error is forgetting to indent your code properly.

main.py
name = 'bobby hadz' if name == 'bobby hadz': print('one') # ⛔️ Badly indented code print('two')

Use a tab to indent your code consistently in the entire if block.

main.py
name = 'bobby hadz' if name == 'bobby hadz': print('one') # ✅ Proper indentation print('two')

properly indent your code

The code in the if statement should be consistently indented.

Never mix tabs and spaces when indenting code in Python because the interpreter often has issues with lines indented using both tabs and spaces.

You can either use tabs when indenting code or spaces, but never mix the two.

# Having an empty block of code

If you need to leave a code block empty before you get to implement it, use a pass statement.

main.py
my_str = '100' if int(my_str) == 100: print('success') else: pass

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

main.py
class Employee: pass

Using a pass statement is necessary because leaving the else block empty would cause a SyntaxError.

main.py
my_str = '100' if int(my_str) == 100: print('success') else: # ⛔️ Error

# The code above the if statement might be causing the error

If the error is not resolved, look at the code that's right above the if statement.

Make sure you haven't forgotten to close a parenthesis ), a square bracket ] or a curly brace }.

main.py
my_str = '100' # ⛔️ SyntaxError: invalid syntax if int(my_str == 100: print('success')

The code sample above has a missing closing parenthesis, which causes the error.

Make sure all your quotes "", parentheses (), curly braces {} and square brackets [] are closed.

main.py
my_str = '100' # ✅ Has a closing parenthesis now if int(my_str) == 100: print('success')

# The else statement should also end with a colon

If you have an if/else statement, make sure the else statement also ends with a colon and its code is indented consistently.

main.py
name = 'Alice' if name == 'Alice': # 👇️ This runs print('success') else: print('failure')

If you have an if or else statement that you haven't yet implemented, use a pass statement.

main.py
name = 'Alice' if name == 'Alice': print('success') else: pass

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

# Make sure you don't have spelling errors

You might get a SyntaxError exception if you have spelling errors.

Python is case-sensitive, so make sure to use lowercase keywords like if and else instead of If or Else.

main.py
my_str = '100' # ✅ Keywords are case-sensitive if int(my_str) == 100: print('success') else: print('condition not met')

I've also written an article on how to check for multiple conditions in an if statement.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.