[Tutor] New to Python - print function - invalid syntax

Cameron Simpson cs at zip.com.au
Tue Aug 5 00:55:53 CEST 2014


On 04Aug2014 13:38, Danny Yoo <dyoo at hashcollision.org> wrote:
>> > difference between the version of Python I'm using (3.4.1) and that which
>> > was in use at the time the book was written (3.1.x) that is responsible
>> > for this error.
>
>Just to double check: how are you confirming what version of Python you're
>using?

In particular, Python 2 will produce a syntax error for your print statement; 
Python 3 should work.

Show a short transcript which also shows your ptyhon version.

Example:

     $ python
     Python 2.7.8 (default, Jul 13 2014, 17:11:32)
     [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
     Type "help", "copyright", "credits" or "license" for more information.
     >>> print("hi", end=" ")
       File "<stdin>", line 1
         print("hi", end=" ")
                        ^
     SyntaxError: invalid syntax

Versus:

     $ python3.4
     Python 3.4.1 (default, May 21 2014, 01:39:38)
     [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
     Type "help", "copyright", "credits" or "license" for more information.
     >>> print("hi", end=" ")
     hi >>>

The "$" is my prompt.

Any Python 3 should accept your print() call.

The default system "python" on most platforms is still Python 2; that may be 
misleading you. In particular, if you're running a script you may need to 
ensure it is run with the correct Python version.

It is perfectly fine to have multiple Python versions installed, BTW. Just make 
sure you're using what you intend.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list