Print without line feed

Question:

How to print something in Python without added line feed at the end?

Something like printf("abc") (no n at the end) in C or process.stdout.write("abc") in Node.js.

Asked By: exebook

||

Answers:

Here a solution try:

print("aze", end = "")

end set the end of line character (and sep the separator of sentences).
You can try:

print("hello", "my name", "is", sep="*", end = "w")
Answered By: Clodion

Python 3:

Use the end parameter of the print function to overwrite its default 'n' value, so that the function will not add a new line at the end:

print(text, end='')

Python 2:

Add a trailing comma to the print statement:

print text,

If you need a solution that works for both Python 2 and Python 3, you should consider importing the print function in Python 2, so you can use the above Python 3 syntax on both. You can do that with the following line at the top of your module:

from __future__ import print_function
Answered By: poke

In python 2 (although this adds an extra space at the end of the text):

print "abc",

In python 3 (or python2 with from __future__ import print_function):

print("abc", end="")

This works in both 2 and 3:

import sys
sys.stdout.write("abc")
Answered By: Steven Kryskalla
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.