2011-08-19 46 views
7

因此,當我嘗試打印Python函數function.__doc__的幫助/信息時,在文檔字符串中出現\n時,控制檯輸出而不是打印新行,打印\n。任何人都可以幫助我禁用/幫助與此?python newline display in console

這是我的輸出:

'divmod(x, y) -> (div, mod)\n\nReturn the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.' 

我想輸出是:

'divmod(x, y) -> (div, mod) 

    Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.' 

P.S:我已經試過這在OS X,Ubuntu的使用Python 2.7。

回答

16

看起來像您檢查交互式shell中的對象,而不是打印它。如果你的意思是打印,請寫下來。

>>> "abc\n123" 
"abc\n123" 
>>> print "abc\n123" 
abc 
123 

在python 3.x打印是一個普通的函數,所以你必須使用()。下面(推薦)將同時在2.x和3.x工作:

>>> from __future__ import print_function 
>>> print("abc\n123") 
abc 
123 
+0

是。所以根據我的理解,爲了'unsee''\ n',我應該使用print來代替只輸入[function] .__ doc__? – armundle

+0

是的。它也會放棄報價。 –

3

您可能會發現更多有用的使用(例如)help(divmod)代替divmod.__doc__

1
In [6]: print divmod.__doc__ 
divmod(x, y) -> (div, mod) 

Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x. 

,但我建議你使用

In [8]: help(divmod) 

或IPython的

In [9]: divmod? 
Type:  builtin_function_or_method 
Base Class: <type 'builtin_function_or_method'> 
String Form:<built-in function divmod> 
Namespace: Python builtin 
Docstring: 
divmod(x, y) -> (div, mod) 

Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.