Python print() without newline

In Python 3.x , you can use the optional end="" argument to the print() function to prevent a newline character from being printed.


without end="" argument:

Python: How to Print Without Newline?

In Python 2.x , you can use a trailing comma:


Old: print x, # Trailing comma suppresses newline

New: print(x, end=" ") # Appends a space instead of a newline

Note that the trailing comma still results in a space being printed at the end of the line , i.e. it's equivalent to using Python 3.x end=" ". To suppress the space character as well, you can use:

to get access to the Python 3 print function.

__future__ is a pseudo-module which programmers can use to enable new language features which are not compatible with the current interpreter. Also, it needs to be the first line of code in your script.

In addition, the print() function in Python 3.x also offers the sep parameter that lets one specify how individual items to be printed should be separated.

Python: Print without newline or space


A built-in file object that is analogous to the interpreter's standard output stream in Python . Unlike print, sys.stdout.write() doesn't switch to a new line after one text is displayed. To achieve this one can employ a new line escape character(\n) .


In fact, wherever a print function is called within the code, it is first written to sys.stdout and then finally on to the screen.

Difference between sys.stdout.write and print

The print is just a thin wrapper that formats the inputs and calls the write function of a given object. It first converts the object to a string (if it is not already a string) and also put a space before the object if it is not the start of a line and a newline character at the end. While using stdout , you need to convert the object to a string yourself (by calling "str", for example) and there is no newline character . The return value for sys.stdout.write() returns the no. of bytes written which also gets printed on the interactive interpret prompt for any expressions enter.
 (C) 2020    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About