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 😊

Leave a Comment