1

I am new to Python. So I am stuck with this issue which I think shouldn't happen if I use Python 3.

I have a list a with (1,2,3,4,5,6). I want to print it out in 123456 form (without any space). So I wrote this code:

a = [1,2,3,4,5,6] 
print(a, sep="")

But I always get this error:

        print(a, sep="") 
                    ^ 
    SyntaxError: invalid syntax

Can someone help me?

Sarvagya Gupta
  • 861
  • 3
  • 13
  • 28
  • 1
    Possible duplicate of [How to print in Python without newline or space?](http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space) – cyfur01 Jun 05 '16 at 02:55
  • Are you *sure* you're running a Python 3 interpreter? That's exactly the error I'd expect from Python 2 (where the parentheses would be surrounding a tuple, rather than being part of a function call). – Blckknght Jun 05 '16 at 06:44
  • I am. I'm running a software called Canopy and I have using the print function like python 3. This is the only place where it's giving me an error. – Sarvagya Gupta Jun 05 '16 at 08:03

2 Answers2

0

I can't work out why that code isn't running, unless it is that you are running a python 2 interpretor (as @blckknght suggests). However, when I want to do this, I use the following code, which works every time:

lst=[1,2,3,4,5,6]
for i in lst: print(i, end="")
print()

Hope this helps.

AvahW
  • 2,083
  • 3
  • 24
  • 30
0

Canopy is python2 based distribution. Quoting official docs (emphasis mine):

An Enthought Canopy subscription provides Python 2.7.11, easy installation and updates of over 400 pre-built and tested scientific and analytic Python packages such as NumPy, Pandas, SciPy, Matplotlib, and IPython, PLUS an integrated analysis environment, graphical debugger, data import tool, and online Python Essentials and Python Development Tools training courses. Academic users may request a free Canopy Academic license.

If you've been using print function in that environment, it had to be prepended with __future__ import

from __future__ import print_function
a = [1,2,3,4,5,6] 
print(a, sep="")

Code above works fine in Python 2.7. Without __future__ import, it yields exact exception as quoted by you.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
  • Thank for the answer. I tried you solution but it didn't give me desired output. I got the following: [1, 2, 3, 4, 5, 6] So tried something on my own: from __future__ import print_function x = int(input()) for i in range(1,x+1): print(i, sep='', end='') I got the following output for input 6: 123456 So I really don't why it doesn't give me output for the list – Sarvagya Gupta Jun 05 '16 at 21:32
  • Try `print(*a, sep="")` if you want to print the items from the list, without the brackets or commas. The other way to do it in Python 2 (without the `print` function) is `print "".join(str(x) for x in a)`. – Blckknght Jun 06 '16 at 02:06