5

I have a strange error using sep, file, (etc.) arguments of python's print() function. I tried to google it out, dag around stackoverflow, and read python's documentation but I came up with nothing. I have attached a simple snippet, I would deeply appreciate any help.

# python
Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("blah"*10, sep=" | ")
  File "<stdin>", line 1
    print("blah"*10, sep=" | ")
                        ^
SyntaxError: invalid syntax
Mr.
  • 9,429
  • 13
  • 58
  • 82

3 Answers3

11

Try:

from __future__ import print_function

first

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • thanks you. it did solve the issue. so how come python documentation (see my update post) specifies otherwise? and, do you know where i can find the relevant documentation for native use of the `print()` function? – Mr. Jul 27 '12 at 09:52
  • 2
    @MrRoth: Read the note on your link ;) – phant0m Jul 27 '12 at 10:01
6

In the 2.x series, print is a statement, while in 3.x it's a function. If you want in 2.6+ to have print as a function, you use from __future__ import print_function as the first import statement.

Expect code to break though

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
0

The print function is specific to Python 3. You have two solutions here:

Write

from __future__ import print_function

so you can use it as specified by cdarke.

Or you use print as a simple statement as it should be with older versions of Python (print "Hello World").

Loïc Lopes
  • 544
  • 8
  • 19
  • The problem with using the old `print` statement is that the separator cannot be changed, unlike the current `print()` function. – cdarke Jan 03 '17 at 20:13