Python 3.3: separation argument (sep) giving an error

↘锁芯ラ 提交于 2019-12-13 04:21:52

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/16635241/python-3-3-separation-argument-sep-giving-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!