Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7.3 : Sep argument showing error

When I am using sep argument in the Python 2.7.3, it is showing error

for example:-

      >>>print ("Hello","World",sep ="**")
           File "<stdin>", line 1
           print ("Hello","World",sep ="**")
                           ^
          SyntaxError: invalid syntax
like image 448
Aman Avatar asked Nov 14 '14 03:11

Aman


2 Answers

You need to type this line first:

from __future__ import print_function

To make print a function, and allow passing arguments like that.

like image 139
parchment Avatar answered Nov 20 '22 14:11

parchment


In Python 2.x, unlike in Python 3.x, print is not a function, but a statement described here. Basically it means that print is treated as a keyword (like for) and is not as powerful as the print function that you know from Python 3.x. In particular, it does not support the sep keyword argument.

You can make print behave similarly to Python 3.x by using the following import:

from __future__ import print_function

If you prefer no to use this import, you can achieve the effect you wanted with:

print "**".join(["Hellow", "World"])
like image 21
Thibaut Avatar answered Nov 20 '22 16:11

Thibaut