Newbie using Python and test script keeps throwing" invalid syntax error"

Here is my script (it keeps erroring on the first line starting at the 4, which is highlighted in orange, and I delete the 4 and try to run the module and get the same error with a newly highlighted character).

Python 3.13.2 (tags/v3.13.2:f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license()” for more information.

print(“Hello World”) # Prints the Words “Hello World”

… # This is a comment
… # This is also a comment
… # This is yet another comment

… “”"
… This is a proper multi-line string.
… This is also a comment-like string.
… This is yet another comment-like string.
… “”"

After pressing F5 I should be able to Save and RUn the script and see it in my shell.

Indentation with whitespace is part of Python’s syntax.
Dedent that multi-line string (shift and tab) to the same level as the print statement.

Matching indentation is critical in Python. This should work for you. Note the indentation. I use the triple double quotes as multi-line comments all the time. Below is correct indentation for the main area of your progrma.

print("Hello world") # Prints the words "Hello world"

"""This is a multi-line
string which can be used as a comment.
"""

Below does not have proper indentation so it gets an error “Unexpected indent”.

print("Hello world") # Prints the words "Hello world"

	"""This is a multi-line
string which can be used as a comment.
	"""

So while the examples are not comments, they are a multi-line string, but still treated as a comment.

In a function indentation also still matters. This is how to correctly indent triple double-quoted strings.

def myfunc():
    """Inputs: nothing
    Outputs: nothing
    Purpose: This prints Hello World.
    """"
    print("Hello world")

And then there’s the issue of backslashes in windows paths if they are put in triple double-quoted strings. This gives an error, probably some type of Unicode error, I forget.

"""Init file is in c:\pyprograms\myprog\main.ini
"""

And to fix it, make the triple double-quoted string a raw string, or don’t put in windows paths there.

r"""Init file is in c:\pyprograms\myprog\main.ini
"""
1 Like

Ok, thanks for the help… but when I rea-dd your proposed script and attempt to run it, this is the error that I am getting. (see image)

You will see the “4” that is highlighted with orange/amber that causes the error and prevents it from being able to see it on my Python terminal. I have considered uninstalling Python and then starting over as well, as this might be some anomaly or something. Thoughts?

Your program contains stuff that shouldn’t be there, stuff that Python printed when used interactively, such as the header and prompt.

It looks like you just saved an interactive session to a file.

That won’t work.

Your program should contain only the actual code.

1 Like

You are working in the Python REPL. I recommend you put your program in a plain text file with a .py file extension. Then you should run your program at the command prompt like this: python myprog.py. This is how normal development, and most tutorials, are done.

but when I rea-dd your proposed script and attempt to run it, this is the error that I am getting

I have no idea what rea-dd is.