[Solved] SyntaxError: cannot assign to literal here in Python

Updated Sep 22, 2023 ⤳ 4 min read

Python raises “SyntaxError: cannot assign to literal here. Maybe you meant ‘==’ instead of ‘=’?” when you assign a value to a literal (e.g., 12, '49.5', 'Sally'). On the other hand, this error occurs if a literal value is the left-hand side operand in a value assignment:


# 🚫 Raises SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
12 = 'Hi I am the value being assigned to 12!'

Additionally, Python provides you a hint, assuming you meant to use the equality operator (==):


 File /dwd/sandbox/test.py, line 2
    12 = 'Hi I am the value being assigned to 12!'
    ^^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

But what's a literal? You may ask. 

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

The term "literal" refers to an actual value in the source code. All the following values are literals:

  • 'someValue'
  • 12.5
  • 23
  • True

Most of the time, the reason is a typo in your code - usually a missing = or invalid syntax in assignment statements.

How to fix "SyntaxError: cannot assign to literal here"

The long error "SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?" occurs under various scenarios:

  1. Wrong left-hand side operand in an assignment statement
  2. Multiple value assignments on a single line
  3. In valid comparison statement

Let's see some examples.

Wrong left-hand side operand in an assignment statements: Assignment statements bind names to values. (e.g., price = 49.99)

 Based on Python syntax and semantics, the left-hand side of the assignment operator (=) should always be an identifier, not a literal value.

Identifiers (a.k.a names) are arbitrary names you use for definitions in the source code, such as variable names, function names, and class names. For instance, in the statement age = 25, age is the identifier.

Python identifiers are based on the Unicode standard annex UAX-31, which describes the specifications for using Unicode in identifiers.

That said, you can't have a literal value (e.g. 12, 45.9, 'stringValue') as the identifier, otherwise, you'll get the "SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?" error. 


# 🚫 Raises SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
'age' = 12
4.9 = score

So if your code looks like the above, here's how to fix it:


# ✅ Correct value assignment
age = 12
score = 4.9

Multiple value assignments on a single line: This is more affecting Python 2.7 users since Python 3 outputs a different error message. On Python 2.7, you'll get the "SyntaxError: can't assign to literal" error, if you initialize your variables like so:


# Raises SyntaxError: can't assign to literal
x = 12, y = 45

To fix the issue, you need to change your code like so:


# ✅ Correct way of having multiple assignments on a single line
x, y = 12, 45

Alternatively, you can use semi-colons (;) to have multiple assignment statements on a single line:


# ✅ Correct way of having multiple assignments on a single line
x = 12; y = 45

Invalid comparison statement : Another cause of "SyntaxError: cannot assign to literal here" is using an invalid sequence of comparison operators while comparing values.

This one is more of a syntax-related mistake and rarely finds its way to the runtime, but it's worth watching.

Imagine you want to check if age is between 12 and 25 (12 <= age < = 25), however, you miss one of the < operators:


age = 12

# 🚫 Raises SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
if 12 = age <= 25:
    print('User is eligible')

In the above code, once Python's interpreter encounters the first =, it assumes you're trying to assign age to 12 (= is a value assignment operator).

Once we add the missing <, the misconception goes away:


age = 12

# ✅ Correct sequence
if 12 <= age <= 25:
    print('User is eligible')

Problem solved!

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`
Exit mobile version