Getting SyntaxError for print with keyword argument end=' '

Python

Python Problem Overview


I have this python script where I need to run gdal_retile.py, but I get an exception on this line:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

The end=' ' is invalid syntax. I am curious as to why, and what the author probably meant to do.

I'm new to python if you haven't already guessed.


I think the root cause of the problem is that these imports are failing and therefore one must contain this import from __future__ import print_function

try: 
   from osgeo import gdal
   from osgeo import ogr
   from osgeo import osr
   from osgeo.gdalconst import *
except:
   import gdal
   import ogr
   import osr
   from gdalconst import *

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

How about this:

#Only for use in Python 2.6.0a2 and later
from __future__ import print_function

This allows you to use the Python 3.0 style print function without having to hand-edit all occurrences of print :)

Solution 3 - Python

In python 2.7 here is how you do it

mantra = 'Always look on the bright side of life'
for c in mantra: print c,

#output
A l w a y s   l o o k   o n   t h e   b r i g h t   s i d e   o f   l i f e

In python 3.x

myjob= 'hacker'
for c in myjob: print (c, end=' ')
#output 
h a c k e r 

Solution 4 - Python

First of all, you're missing a quote at the beginning but this is probably a copy/paste error.

In Python 3.x, the end=' ' part will place a space after the displayed string instead of a newline. To do the same thing in Python 2.x, you'd put a comma at the end:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),

Solution 5 - Python

I think he's using Python 3.0 and you're using Python 2.6.

Solution 6 - Python

This is just a version thing. Since Python 3.x the print is actually a function, so it now takes arguments like any normal function.

The end=' ' is just to say that you want a space after the end of the statement instead of a new line character. In Python 2.x you would have to do this by placing a comma at the end of the print statement.

For example, when in a Python 3.x environment:

while i<5:
    print(i)
    i=i+1

Will give the following output:

0
1
2
3
4

Where as:

while i<5:
    print(i, end = ' ')
    i=i+1

Will give as output:

0 1 2 3 4

Solution 7 - Python

It looks like you're just missing an opening double-quote. Try:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

Solution 8 - Python

I think the author probably meant:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

He's missing an initial quote after print(.

Note that as of Python 3.0, print is a function as opposed to a statement, if you're using older versions of Python the equivalent would be:

print "Building internam Index for %d tile(s) ..." % len(inputTiles)

The end parameter means that the line gets ' ' at the end rather than a newline character. The equivalent in earlier versions of Python is:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),

(thanks Ignacio).

Solution 9 - Python

USE :: python3 filename.py

I had such error , this occured because i have two versions of python installed on my drive namely python2.7 and python3 . Following was my code :

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

when i run it by the command python lines.py I got the following error

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

when I run it by the command python3 lines.py I executed successfully

Solution 10 - Python

For python 2.7 I had the same issue Just use "from future import print_function" without quotes to resolve this issue.This Ensures Python 2.6 and later Python 2.x can use Python 3.x print function.

Solution 11 - Python

Try this one if you are working with python 2.7:

from __future__ import print_function

Solution 12 - Python

Compatible with both Python 2 & 3:

sys.stdout.write('mytext')

Compatible with only Python 2

print 'mytext',

Compatible with only Python 3

print('mytext', end='')

Solution 13 - Python

Basically, it occurs in python2.7 here is my code of how it works:

  i=5
while(i):
	print i,
	i=i-1
Output:
5 4 3 2 1

Solution 14 - Python

I had faced this error, and occurred because of using version 2.7.15 but end='' works with version 3 only.

Example:

import array as arr
a=arr.array('i',[1,2,3,4])
for x in range(0,3):
    print ("%d" % a[x], end=' ')

Output : Showing error "Invalid Syntax" as using version 2.7

For Version 2.7.15 use this syntax

import array as arr
a=arr.array('i',[1,2,3,4])
for x in range(0,3):
    print ("%d" % a[x]),

Output : 1 2 3

Solution 15 - Python

Even I was getting that same error today. And I've experienced an interesting thing. If you're using python 3.x and still getting the error, it might be a reason:

> You have multiple python versions installed on same drive. And when > you're presing the f5 button the python shell window (of ver. < 3.x) > pops up

I was getting same error today, and noticed that thing. Trust me, when I execute my code from proper shell window (of ver. 3.x), I got satisfactory results

Solution 16 - Python

we need to import a header before using end='', as it is not included in the python's normal runtime.

from __future__ import print_function

it shall work perfectly now

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHathView Question on Stackoverflow
Solution 1 - PythonAlan PlumView Answer on Stackoverflow
Solution 2 - PythonbadpView Answer on Stackoverflow
Solution 3 - PythonRitesh KarwaView Answer on Stackoverflow
Solution 4 - PythoninterjayView Answer on Stackoverflow
Solution 5 - PythonCharles BeattieView Answer on Stackoverflow
Solution 6 - PythonkdperspectiveView Answer on Stackoverflow
Solution 7 - PythonWill RobinsonView Answer on Stackoverflow
Solution 8 - PythonDominic RodgerView Answer on Stackoverflow
Solution 9 - PythonSubbuView Answer on Stackoverflow
Solution 10 - PythonViraj PatilView Answer on Stackoverflow
Solution 11 - PythonscofieldView Answer on Stackoverflow
Solution 12 - PythonJellicleCatView Answer on Stackoverflow
Solution 13 - Pythonsanthosh bandaruView Answer on Stackoverflow
Solution 14 - PythonSaurabh sharmaView Answer on Stackoverflow
Solution 15 - PythonBiswajit PaulView Answer on Stackoverflow
Solution 16 - PythonsukantkView Answer on Stackoverflow