Why am I getting a syntax error on this code

Python ver 3.9.4

msg = "I love learning to use Python."
print(msg)

Thank you, new to Python, kinda new to coding, sorry if i posted in wrong area

I’m not sure, because copy/pasting your code directly into the interactive prompt works just fine for me :slight_smile:

Rather than summarizing what went wrong as “a syntax error” it’s usually best to copy/paste exactly the code that you used and the error you got along with a description of how you ran the code so that others can see what you saw and give better help. Barring that, the best I can do here is “try again, it ought to work”.

Welcome to Python :). You posted in exactly the right place.

I’m not sure, because copy/pasting your code directly into the
interactive prompt works just fine for me :slight_smile:

Maybe the OP hand typed correct code into the forum :slight_smile: While reading by
eye from incorrect code :frowning:

Rather than summarizing what went wrong as “a syntax error” it’s
usually best to copy/paste exactly the code that you used and the error
you got along with a description of how you ran the code so that others
can see what you saw and give better help. Barring that, the best I
can do here is “try again, it ought to work”.

Aye. In particular, note that Python quotes are either ASCII double
quote " or ASCII single quote '. Sometimes people type some kind of
“smart quote”, cut/paste from an badly transcribed example. For example,
the Unicode open and close quotes (distinct characters) are not valid
Python quotes.

Cheers,
Cameron Simpson cs@cskk.id.au

Sometimes the Python interpreter gets confused about errors on the
previous line, and reports the syntax error in the next line:

# Python 3.9
>>> x = [1, 2, 3
... msg = "I love learning to use Python."
  File "<stdin>", line 2
    msg = "I love learning to use Python."
    ^
SyntaxError: invalid syntax

The actual error is the previous line, the x = line is missing a
closing bracket.