Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python newline display in console

Tags:

python

So, when I try to print help/info of Python functions function.__doc__, the console output instead of printing a newline when \n occurs in the doc string, prints \n. Can anyone help me with disabling/helping out with this?

This is my output:

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

What I would like the output to be:

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

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

P.S: I have tried this on OS X, Ubuntu with Python 2.7.

like image 419
armundle Avatar asked Aug 19 '11 00:08

armundle


3 Answers

Looks like you inspected the object in the interactive shell, not printed it. If you mean print, write it.

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

In python 3.x print is an ordinary function, so you have to use (). The following (recommended) will work in both 2.x and 3.x:

>>> from __future__ import print_function
>>> print("abc\n123")
abc
123
like image 139
Jürgen Strobel Avatar answered Sep 23 '22 13:09

Jürgen Strobel


You might find it more helpful to use (for example) help(divmod) instead of divmod.__doc__.

like image 21
Ned Batchelder Avatar answered Sep 19 '22 13:09

Ned Batchelder


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

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

but i suggest you use

In [8]: help(divmod)

or in 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.
like image 20
timger Avatar answered Sep 23 '22 13:09

timger