This post will discuss how to print a text without a newline in Python.

1. Using print() function

The built-in function print() is widely used in Python 3.x to print objects to the text stream. Consider the following code example, which demonstrates a simple usage of the print() function:

Download  Run Code

 
The above code will print the string Hello and World in two different lines. This is because the print() function has an optional end argument, which defaults to a newline (\n). To avoid newline at the end, you can assign end to an empty string, as shown below:

Download  Run Code

In Python 2, the print functionality has a statement dedicated to it. To interpret print(), you need to specify the from __future__ import print_function compiler directive. The following code demonstrates this usage of the print statement.

Download  Run Code

3. Using sys.stdout.write() function

Alternatively, you can use stdout for the output of print(). To write text to stdout, use sys.stdout.write() function.

Download  Run Code

That’s all about printing text in Python without a new line.