Trying to print list items on individual lines as simply as possible following this:
https://www.geeksforgeeks.org/print-lists-in-python-4-different-ways/
>>> myVar = [1, 2, 3, 4, 5]
>>> print(*myVar)
File "<stdin>", line 1
print(*myVar)
^
SyntaxError: invalid syntax
I must use Python 2.7.8, so I guess I have to translate that without () but I fail at this:
>>> print *myVar
File "<stdin>", line 1
print *myVar
^
SyntaxError: invalid syntax
So, is this a syntax issue? Or is there a better way to do this on v2.7.8?
Python 2's
printstatement doesn't support argument unpacking (because it's not a function, so it has no arguments), but you can opt-in to using Python 3'sprintfunction even in Python 2, by adding the following to the very top of your file (before any line aside from a shebang line and file encoding comments I believe;__future__imports are weird, and since they change the compiler behavior, they need to occur before any other code):Once you do that, the
printstatement ceases to exist for that script, and you can use Python 3'sprintfunction normally, e.g. for your desired task of printing the values ofmyVar, one per line, you'd do: