Home > Software engineering >  Cant assign to literal, Python
Cant assign to literal, Python

Time:10-19

When l try to print a string l get the message that it cant assign to literal and l was wondering if anyone could point out why this is happening. I am try to split the message below when l get the syntax error.

Code:

s = "Hakuna Mata", character = 'a'
s = s.split(character)
print(s)

CodePudding user response:

Multi assignment in Python is done differently:

s, character = "Hakuna Mata", "a"

CodePudding user response:

you have to use semicolon; instead of , to write two lines of code in the same line or write them on new lines.

s = "Hakuna Mata";character = 'a'

Or

s = "Hakuna Mata"
character = 'a'

CodePudding user response:

The issue is your first line:

s = "Hakuna Mata", character = 'a'

This kind of formatting doesn't work, you will need to simply split this into two lines

s = "Hakuna Mata"
character = 'a'

Remember that python cares a lot about things such as indentation for example, so you cannot just use statements like that.

If you want to do it in one line, user2390182's answer is the most pythonic method, but Python learner's method is how you would do it if you want to have multiple statements in one line (or if you want to make a shorter code with an if-statement such as:

if a: x();y();z()

CodePudding user response:

search google by error message. it's good way improve coding ability.

s, character = "Hakuna Mata", 'a'
s = s.split(character)
print(s)

Reference: https://stackoverflow.com/a/35595550/17190006

  • Related