158

I am new at programming with python, and I am trying to print out with a separator and end but it is still giving me a syntax error.

I am using python 2.7.

Here is my code:

from __future__ import print_function
import sys, os, time

for x in range(0,10):
    print x, sep=' ', end=''
    time.sleep(1)

And here is the error:

$ python2 xy.py
  File "xy.py", line 5
    print x, sep=' ', end=''
          ^
SyntaxError: invalid syntax
$
vy32
  • 28,461
  • 37
  • 122
  • 246
UHMIS
  • 1,701
  • 2
  • 11
  • 5
  • 7
    You imported print as a function but *you're still treating it as a statement*. – jonrsharpe Aug 16 '15 at 07:32
  • 5
    you can't call print without the parenthesis because you have changed print to be a function print(args) – Charlie Parker Aug 28 '16 at 17:36
  • 2
    See also [What is `__future__` in Python used for and how/when to use it, and how it works](https://stackoverflow.com/q/7075082/113116) – Helen May 30 '18 at 07:52
  • You are still using python 2 print syntax, but you imported the print function from the future (aka the print function inside python 3). It replaces the old print syntax with the new syntax, thus creating an error – CrazyVideoGamer Jan 07 '21 at 16:23
  • Also, I don't know why you are still using python 2, when python 3 is out, but whatever. – CrazyVideoGamer Jan 07 '21 at 16:24

1 Answers1

251

First of all, from __future__ import print_function needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print as a function now. That's the whole point of from __future__ import print_function; to bring the print function from Python 3 into Python 2.6+.

from __future__ import print_function

import sys, os, time

for x in range(0,10):
    print(x, sep=' ', end='')  # No need for sep here, but okay :)
    time.sleep(1)

__future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning. From the documentation:

A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.

The documentation also mentions that the only things that can precede a __future__ statement are the module docstring, comments, blank lines, and other future statements.

Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • Thanks... However, now it is printing 0123456789 instead of 0 1 2 3 4 5 6 7 8 9. how do I solve that? – UHMIS Aug 16 '15 at 07:16
  • 2
    @UHMIS, do `end=' '`. – Cyphase Aug 16 '15 at 07:23
  • @UHMIS, it's not useless, it's just not useful in that particular code, because you're only printing one thing per call to `print`. If you were doing, for example, `print(x, x**2, sep=' ', end=' ')`, it would be useful there because it's putting a separator between each item (in this case, `x` and `x**2`). Of course, the default `sep` is `' '`, so you don't have to specify that anyway. – Cyphase Aug 16 '15 at 07:33
  • @AvinashRaj, can you show me what you mean? – Cyphase Aug 16 '15 at 07:34
  • @AvinashRaj, let's talk about your answer in the comments to your answer, to make it easier for posterity :). – Cyphase Aug 16 '15 at 07:36
  • @Cyphase then why he says that my code is working? – Avinash Raj Aug 16 '15 at 07:47
  • 1
    @AvinashRaj, I don't know; you'd have to ask UHMIS. But as I said in a comment to your answer, perhaps OP made a change and didn't mention it. And OP's first comment _was_ that there was still an error. – Cyphase Aug 16 '15 at 07:50
  • @not2qubit I reverted the edit you just made, and I wanted to let you know why, since I know it was done in good faith. While the table was obviously related to `__future__`, it didn't really help answer the question or directly expand on the answer; it was just some extraneous related info. Also I'm not sure what exactly your intention was behind italicizing "docstring, comments, blank lines". Please feel free to respond if you want to discuss it, or if you'd like to comment with that link to the `__future__` docs. Thanks for wanting to improve my answer. – Cyphase Nov 20 '18 at 02:35
  • @Cyphase No problem. I just wanted to show what other features was available, along with when they were introduced, since that would be a natural followup question and would have helped clarify what they are used for. – not2qubit Nov 20 '18 at 10:48