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.
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='')
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
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")
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")