Search code examples
pythonseparatorpython-3.3

Python 3.3: separation argument (sep) giving an error


I am very new to programming and I'm starting out with Python. I tried to look up my question here but didn't really find anything.

I'm trying to work a very simple print command but I'm getting an error for some reason that I don't understand.

last = 'smith'
middle = 'paul'
first = 'john'
print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')

According to the answer in the book, this should be right, but every time I try to run it, I get an error with the 'sep':

print(first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
                                                                     ^
SyntaxError: invalid syntax

Can someone tell me what I'm doing wrong. for what it's worth I'm using PyScripter.

[EDIT]

Thanks for that. I found out that I'm using Python 2.7.3 instead of 3.3. So I looked up the manual to see how the separator works. It seems to me that the only difference is with the square bracket. The manual describes the print function as :

print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout]) 

So I changed my print command and added the square bracket:

print ([first.capitalize(),middle.capitalize(),last.capitalize()] [, sep='\t'])

but unfortunately this doesn't work either as I get an error that highlights the square brackets around sep='\t'. Even when I take the brackets out, the error doesn't go away.

I'm not sure what I'm doing wrong, it seems like it should be very simple.


Solution

  • You aren't actually using Python 3, you just think you are. Try:

    import sys
    print(sys.version)
    

    and see what comes out. The Python 2 print ... statement (not print(...) function in Python 3) interprets this as

    print (first.capitalize(), middle.capitalize(), last.capitalize(), sep='\t')
    

    which is trying to print a tuple with a keyword argument, thus the syntax error on sep