Syntaxerror: cannot assign to literal here in Python

How to fix syntaxerror: cannot assign to literal here in Python?

If you are struggling to resolve this SyntaxError, you must continue reading.

In this article, we’ll explore the cannot assign to literal a SyntaxError that bothers you while running your code in Python.

What is “syntaxerror cannot assign to literal”?

The error message syntaxerror: cannot assign to literal occurs when you are trying to assign a value to a literal value namely, a string, a list, a boolean, and an integer or number. 

You can’t assign to literal values namely string or numbers, If you insist to do so, then, you’ll face this error.

For example:

43 = 'xyz'

Output:

File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1
    43 = 'xyz'
    ^^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

Another example:

'website' = 'Itsourcecode'

Output:

File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1
    'website' = 'Itsourcecode'
    ^^^^^^^^^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

The error is triggered because we are trying to assign a value to two literals (the integer 43 and the string website).

Take note: The left side of the = operator needs to be variable, and the value is on the right side of the assignment.

Why does the “cannot assign to literal” SyntaxError occur in Python?

The syntaxerror: can’t assign to literal here error message occurs when you try to assign a value to a literal value, which is not allowed in Python.

Here are the following reasons which affect to your that results in this error message:

Assigning a value to a string Literal

For example:

"Hi, Welcome to Itsourcecode!" = "Greetings!"

❌ Modifying a numeric literal

Numeric literals, namely integers or floating-point numbers, are also immutable in most programming languages.

Thus, trying to allocate a new value to a numeric literal will result in this error.

For example:

43 = 16

❌ Trying to change a boolean literal
Boolean literals, which represent true or false values, cannot be modified directly.

For example:


True = False

How to fix “syntaxerror: cannot assign to literal here” in Python?

To fix the syntaxerror can’t assign to literal here in Python. You need to create a variable and assign the literal value to it. It will allow you to use the variable instead of the literal in your code.

You can use the following solutions to help you resolve this error message.

Solution 1: Use a variable instead of a literal

If you declare a variable, ensure the variable name is on the left-hand side. And the value is on the right-hand side of the assignment (=).

For example:

x = 43
print(x)

Output:

43

Another example:

website = 'Itsourcecode'
print(website)

Note: The variable names should be wrapped in quotes because it is a string literal.

Output:

Itsourcecode

Solution 2: Use the correct assignment operator

Ensure you are using the correct assignment operator (=) and not the equality operator (==). The = operator assigns a value to a variable, while the == operator compares two values for equality.

Solution 3: Modify the Value using Mutable Data Types

If the literal is part of a mutable data type, such as a list or dictionary, you can modify its value within the data structure.


sample_list = ["Hi", "Welcome to Itsourcecode"]
sample_list[1] = "Greetings"
print(sample_list)

Solution 4: Remove the quotes to declare the variable correctly

If you encounter this error while assigning a value to a literal in a for loop. You should check the following example:

sample_list = ['it', 'source', 'code']

for 'sample' in sample_list:
    print(sample)

As you can see, we wrapped the sample variable in quotes which makes it a string literal.

Output:

 File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 3
    for 'sample' in sample_list:
        ^^^^^^^^
SyntaxError: cannot assign to literal

To resolve this error, we have removed the quotes to declare the variable correctly.

Corrected code:

sample_list = ['it', 'source', 'code']

for sample in sample_list:
    print(sample)

Output:

it
source
code

Solutions 5: Check for typos

Ensure you are not accidentally trying to assign a value to a literal due to a typo.

For example, ensure you are not accidentally typing 1 = a instead of a = 1.

Conclusion

In conclusion, the error message syntaxerror: cannot assign to literal occurs when you are trying to assign a value to a literal value namely, a string, a list, a boolean, and an integer or number. 

To fix the syntaxerror can’t assign to literal here in Python. You need to create a variable and allocate the literal value to it. It will allow you to use the variable instead of the literal in your code.

This article already discussed what this error is all about and multiple ways to resolve this error.

By executing the solutions above, you can master this SyntaxError with the help of this guide.

You could also check out other SyntaxError articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

What is Python SyntaxError and what causes it?

SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.

How do I fix ‘unexpected EOF while parsing’?

Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.

Why does my syntax error point to a line that looks correct?

The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.

Why does ‘print x’ raise SyntaxError in Python 3?

Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.

Where can I find more SyntaxError fixes?

Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.

Leave a Comment