Getting SyntaxError for print with keyword argument end=' '
Are you sure you are using Python 3.x? The syntax isn't available in Python 2.x because print
is still a statement.
print("foo" % bar, end=" ")
in Python 2.x is identical to
print ("foo" % bar, end=" ")
or
print "foo" % bar, end=" "
i.e. as a call to print with a tuple as argument.
That's obviously bad syntax (literals don't take keyword arguments). In Python 3.x print
is an actual function, so it takes keyword arguments, too.
The correct idiom in Python 2.x for end=" "
is:
print "foo" % bar,
(note the final comma, this makes it end the line with a space rather than a linebreak)
If you want more control over the output, consider using sys.stdout
directly. This won't do any special magic with the output.
Of course in somewhat recent versions of Python 2.x (2.5 should have it, not sure about 2.4), you can use the __future__
module to enable it in your script file:
from __future__ import print_function
The same goes with unicode_literals
and some other nice things (with_statement
, for example). This won't work in really old versions (i.e. created before the feature was introduced) of Python 2.x, though.
Python SyntaxError: invalid syntax end=''
It seems like you're using Python 2.x, not Python 3.x.
Check your python version:
>>> import sys
>>> sys.version
'2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]'
>>> print(1, end='')
File "<stdin>", line 1
print(1, end='')
^
SyntaxError: invalid syntax
In Python 3.x, it should not raise Syntax Error:
>>> import sys
>>> sys.version
'3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]'
>>> print(1, end='')
1>>>
Python : How to make the 'end' keyword argument in print function to stop functioning for this purpose
Just print nothing.
for ...:
print(..., end='')
print()
for ...:
print(..., end='')
Meaning of end='' in the statement print( \t ,end='')?
The default value of end
is \n
meaning that after the print
statement it will print a new line. So simply stated end
is what you want to be printed after the print
statement has been executed
Eg: - print ("hello",end=" +")
will print hello +
Related Topics
Getting List of Parameter Names Inside Python Function
How to Find an Element That Contains Specific Text in Selenium Webdriver (Python)
How to Execute Python Scripts in Windows
Why Can't Non-Default Arguments Follow Default Arguments
Error "Filename.Whl Is Not a Supported Wheel on This Platform"
How to Capture Stdout Output from a Python Function Call
How to Print Pandas Dataframe Without Index
Django. Override Save for Model
Resetting Generator Object in Python
Django Urls Typeerror: View Must Be a Callable or a List/Tuple in the Case of Include()
Hash Function in Python 3.3 Returns Different Results Between Sessions
Taking Multiple Integers on the Same Line as Input from the User in Python
Setting Smaller Buffer Size for Sys.Stdin
Apply Pandas Function to Column to Create Multiple New Columns
Is There a Label/Goto in Python