1

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.

exebook
  • 32,014
  • 33
  • 141
  • 226

3 Answers3

6

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
poke
  • 369,085
  • 72
  • 557
  • 602
3

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")
Steven Kryskalla
  • 14,179
  • 2
  • 40
  • 42
1

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")
Clodion
  • 1,017
  • 6
  • 12