Python新行显示在控制台中

时间:2011-08-19 00:46:42

标签: python

因此,当我尝试打印Python函数function.__doc__的帮助/信息时,控制台输出而不是在文档字符串中出现\n时打印换行符,打印\n。任何人都可以帮助我禁用/帮助解决这个问题吗?

这是我的输出:

'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上试过这个,用Python 2.7试过Ubuntu。

3 个答案:

答案 0 :(得分:19)

看起来你在交互式shell中检查了对象,而不是打印它。如果你的意思是打印,请写下来。

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

在python 3.x中,print是一个普通的函数,所以你必须使用()。以下(推荐)将适用于2.x和3.x:

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

答案 1 :(得分:3)

您可能会发现使用(例如)help(divmod)代替divmod.__doc__更有帮助。

答案 2 :(得分: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.