Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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 

$

1 Answer

0 votes
by (106k points)

__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.

from __future__ import print_function 

import sys, os, time 

for x in range(0,10): 

print(x, sep=' ', end='') 

time.sleep(1)

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Feb 19, 2021 in Java by Jake (7k points)
0 votes
4 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...