Python print new line in console

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.

agf

164k40 gold badges278 silver badges232 bronze badges

asked Aug 19, 2011 at 0:46

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

answered Aug 19, 2011 at 0:49

2

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

answered Aug 19, 2011 at 0:52

Ned BatchelderNed Batchelder

351k71 gold badges552 silver badges649 bronze badges

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.

answered Aug 19, 2011 at 1:27

timgertimger

9342 gold badges11 silver badges30 bronze badges

Python print() built-in function is used to print the given content inside the command prompt. The default functionality of Python print is that it adds a newline character at the end.

In this Python tutorial, you will learn:

  • Working of Normal print() function
  • How to print without a newline in Python?
  • Print Without Newline in Python 2.x
  • Using Python sys module
  • Using print() to print a list without a newline
  • Printing the star(*) pattern without newline and space

Working of Normal print() function

The print() function is used to display the content in the command prompt or console.

Here is an example that shows the working of Python print without newline function.

print("Hello World")
print("Welcome to Guru99 Tutorials")

Output

Hello World
Welcome to Guru99 Tutorials

In the given output, you can see that the strings that we have given in print() are displayed on separate lines. The string “Hello World ” is printed first, and “Welcome to Guru99 Tutorials” is printed on the next line.

From Python 3+, there is an additional parameter introduced for print() called end=. This parameter takes care of removing the newline that is added by default in print().

In the Python 3 print without newline example below, we want the strings to print on same line in Python. To get that working just add end=”” inside print() as shown in the example below:

print("Hello World ", end="")
print("Welcome to Guru99 Tutorials")

Output:

Hello World Welcome to Guru99 Tutorials

We are getting the output that we wanted, but there is no space between the strings. The string Hello World and Welcome to Guru99 Tutorials are printed together without any space.

In order to add space or special character or even string to be printed, the same can be given to the end=”” argument, as shown in the example below.

print("Hello World ", end=" ")
print("Welcome to Guru99 Tutorials")

So I here we have added one space to the end argument, for example (end=” “). Now, if you see the output, you should see a space between Hello World and Welcome to Guru99 Tutorials.

Output:

Hello World  Welcome to Guru99 Tutorials

It’s not only space that you can give to the end argument, but you can also specify a string you wish to print between the given strings. So here is an example of it

print("Hello World ", end="It's a nice day! ")
print("Welcome to Guru99 Tutorials")

Output:

Hello, World It's a nice day!  Welcome to Guru99 Tutorials

To get the strings to print without a newline, in python2.x you will have to add a comma (,) at the end of the print statement as shown below:

print "Hello World ", 
print "Welcome to Guru99 Tutorials."

Output:

Hello World  Welcome to Guru99 Tutorials

Using Python sys module

Yet another method that you can use to print without newline in Python is the built-in module called sys.

Here is a working example that shows how to make use of the sys module to print without newline Python strings.

To work with the sys module, first, import the module sys using the import keyword. Next, make use of the stdout.write() method available inside the sys module, to print your strings.

import sys

sys.stdout.write("Hello World ")
sys.stdout.write("Welcome to Guru99 Tutorials")

Output:

Hello World Welcome to Guru99 Tutorials

Using print() to print a list without a newline

Consider a list of items for example: mylist = [“PHP”, JAVA”, “C++”, “C”, “PHYTHON”] and you want to print the values inside the list using for-loop. So here you can make use of print() to display the values inside the list as shown in the example below:

mylist = ["PHP", "JAVA", "C++", "C", "PHYTHON"]
for i in mylist:
	print(i)

Output:

PHP
JAVA
C++
C
PHYTHON

The output shows the list items, each printed one after another, on a new line. What if you want all the items on the list in the same line? For that, make use of end argument inside print() that will remove new line in Python and print all items of the list in the same line.

mylist = ["PHP", "JAVA", "C++", "C", "PYTHON"]
for i in mylist:
	print(i, end=" ")

Output:

PHP JAVA C++ C PHYTHON

Printing the star(*) pattern without newline and space

The example of Python print without newline will make use of print() function to print stars(*) on the same line using for-loop.

for i in range(0, 20):
    print('*', end="")

Output:

********************

Summary:

  • Python print() built-in function is used to print the given content inside the command prompt. The default functionality of Python print is that it adds a newline character at the end.
  • From Python 3+, there is an additional parameter introduced for print() called end=. The param end= takes care of removing the newline that is added by default in print().
  • In python2.x you can add a comma (,) at the end of the print statement that will remove newline from print Python.
  • Another method that you can use for Python print no newline is the built-in module called sys.

Learn our next tutorial about Python Timer Function

Does Python print add newline?

In Python, the built-in print function is used to print content to the standard output, which is usually the console. By default, the print function adds a newline character at the end of the printed content, so the next output by the program occurs on the next line.

What does \n and \t do in Python?

Learn More. In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

How do you code a line break in Python?

The new line character in Python is used to mark the end of a line and the beginning of a new line. To create a string containing line breaks, you can use one of the following. Newline code \n(LF), \r\n(CR + LF). Triple quote ”' or “””.

Tải thêm tài liệu liên quan đến bài viết Python print new line in console