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:
In Python 2.x , you can use a trailing comma:
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.
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.
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.