2012-07-27 87 views
5

我有一個奇怪的錯誤,使用sep,file,(等)python的print()函數的參數。 我試圖谷歌它,達到了周圍的計算器,並閱讀python's documentation,但我沒有想出什麼。 我附上了一個簡單的代碼片段,我將非常感謝任何幫助。Python 2.7打印()錯誤

# python 
Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> print("blah"*10, sep=" | ") 
    File "<stdin>", line 1 
    print("blah"*10, sep=" | ") 
         ^
SyntaxError: invalid syntax 

回答

11

嘗試:

from __future__ import print_function 

第一

+0

謝謝。它確實解決了這個問題。那麼python文檔(請參閱我的更新文章)是如何指定的?並且,你知道我在哪裏可以找到相關的文檔,用於本地使用'print()'函數嗎? – MrRoth 2012-07-27 09:52:37

+2

@MrRoth:閱讀鏈接上的註釋;) – phant0m 2012-07-27 10:01:19

+1

@MrRoth你應該接受這個答案。 – Ash 2013-01-09 16:41:08

6

在2.x系列,print是一個聲明,而在3.x中它是一個函數。如果您希望在2.6+中具有print作爲函數,則可以使用from __future__ import print_function作爲第一個導入語句。

期待碼打破雖然

0

打印功能是針對Python 3 你這裏有兩種解決方案:

from __future__ import print_function 

,所以你可以使用它作爲指定由cdarke。

或者您使用print作爲一個簡單的語句,因爲它應該與舊版本的Python(print "Hello World")一樣。

+0

使用舊的'print'語句的問題是分隔符不能改變,不像當前的'print()'函數。 – cdarke 2017-01-03 20:13:43