# Why am I getting a syntax error on this code

**URL:** <https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495>\
**Category:** Python Help\
**Tags:** help\
**Created:** [April 30, 2021, 8:20pm UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495 "2021-04-30T20:20:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/scott/32/4066_2.png) [@Scott](https://discuss.python.org/u/Scott)\
**Post date:** [April 30, 2021, 8:20pm UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495/1 "2021-04-30T20:20:55Z")

</div>

Python ver 3.9.4

```auto
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

---

<div class="post-metadata">

**Author:** ![zware](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zware/32/58_2.png) [@zware](https://discuss.python.org/u/zware)\
**Post date:** [April 30, 2021, 8:45pm UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495/2 "2021-04-30T20:45:17Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [April 30, 2021, 11:38pm UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495/3 "2021-04-30T23:38:59Z")

</div>

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

Maybe the OP hand typed correct code into the forum 🙂 While reading by  
eye from incorrect code ☹

> 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](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [May 1, 2021, 12:18am UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495/4 "2021-05-01T00:18:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joehaffey](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/joehaffey/32/6322_2.png) [@joehaffey](https://discuss.python.org/u/joehaffey)\
**Post date:** [February 21, 2022, 11:50pm UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495/5 "2022-02-21T23:50:46Z")

</div>

scott,

try putting everything into a main function. i tried this and it worked first try:

def main():  
msg = (‘i love learning to use python’)  
print(msg)

```
return

```

main()

---

<div class="post-metadata">

**Author:** ![joehaffey](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/joehaffey/32/6322_2.png) [@joehaffey](https://discuss.python.org/u/joehaffey)\
**Post date:** [February 22, 2022, 12:06am UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495/6 "2022-02-22T00:06:48Z")

</div>

cameron, another noob here. i read your response to this post and you mention the interactive prompt. are you referring to the command line interpreter or is this something else?

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [February 22, 2022, 1:03am UTC](https://discuss.python.org/t/why-am-i-getting-a-syntax-error-on-this-code/8495/7 "2022-02-22T01:03:38Z")

</div>

> cameron, another noob here. i read your response to this post and you  
> mention the interactive prompt. are you referring to the command line  
> interpreter or is this something else?

For me, the interpret invoked interactively from the command line.  
Example:

```
CSS[~/hg/css-pypi(hg:pypi)]fleet2*> py3
Python 3.9.10 (main, Jan 15 2022, 11:48:15)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more 
information.
>>>

```

In the above, `CSS[~/hg/css-pypi(hg:pypi)]fleet2*>` is my shell prompt,  
which is what I mean if I say “the command line”. For commands like  
`ls`, `rsync`, etc.

The string `py3` above is me typing the command:

```
py3

```

which is a little shell script of my own to invoke my “current”  
preferred python, usually from a personal virtualenv. It ends up running  
`python3`. With no arguments, that’s the Python 3 interactive mode:

```
Python 3.9.10 (main, Jan 15 2022, 11:48:15)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more 
information.
>>>

```

The `>>>` above is the Python interactive prompt, where you can type  
Python syntax. That’s what I mean by “the Python interactive prompt”.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)
