用cons显示Python换行符

2024-09-28 21:53:43 发布

您现在位置: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.'

附:我已经在OSX,UbuntuwithPython2.7上试过了。


Tags: the函数字符串文档div信息moddoc
3条回答

看起来你检查了交互shell中的对象,而不是打印它。如果你是指印刷品,就写下来。

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

在Python3.x中,print是一个普通函数,因此必须使用()。以下(推荐)在2.x和3.x中都适用:

>>> from __future__ import print_function
>>> print("abc\n123")
abc
123
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)

或者在伊普顿

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.

您可能会发现使用help(divmod)而不是divmod.__doc__更有帮助。

相关问题 更多 >