How can I suppress the newline after a print statement?
The question asks: "How can it be done in Python 3?"
Use this construct with Python 3.x:
for item in [1,2,3,4]:
print(item, " ", end="")
This will generate:
1 2 3 4
See this Python doc for more information:
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
--
Aside:
in addition, the print()
function also offers the sep
parameter that lets one specify how individual items to be printed should be separated. E.g.,
In [21]: print('this','is', 'a', 'test') # default single space between items
this is a test
In [22]: print('this','is', 'a', 'test', sep="") # no spaces between items
thisisatest
In [22]: print('this','is', 'a', 'test', sep="--*--") # user specified separation
this--*--is--*--a--*--test
How to print without a newline or space
In Python 3, you can use the sep=
and end=
parameters of the print
function:
To not add a newline to the end of the string:
print('.', end='')
To not add a space between all the function arguments you want to print:
print('a', 'b', 'c', sep='')
You can pass any string to either parameter, and you can use both parameters at the same time.
If you are having trouble with buffering, you can flush the output by adding flush=True
keyword argument:
print('.', end='', flush=True)
Python 2.6 and 2.7
From Python 2.6 you can either import the print
function from Python 3 using the __future__
module:
from __future__ import print_function
which allows you to use the Python 3 solution above.
However, note that the flush
keyword is not available in the version of the print
function imported from __future__
in Python 2; it only works in Python 3, more specifically 3.3 and later. In earlier versions you'll still need to flush manually with a call to sys.stdout.flush()
. You'll also have to rewrite all other print statements in the file where you do this import.
Or you can use sys.stdout.write()
import sys
sys.stdout.write('.')
You may also need to call
sys.stdout.flush()
to ensure stdout
is flushed immediately.
Python: avoid new line with print command
In Python 3.x, you can use the end
argument to the print()
function to prevent a newline character from being printed:
print("Nope, that is not a two. That is a", end="")
In Python 2.x, you can use a trailing comma:
print "this should be",
print "on the same line"
You don't need this to simply print a variable, though:
print "Nope, that is not a two. That is a", x
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 end=" "
in Python 3. To suppress the space character as well, you can either use
from __future__ import print_function
to get access to the Python 3 print function or use sys.stdout.write()
.
Print statements without new lines in python?
This can be easily done with the print() function with Python 3.
for i in x:
print(i, end="") # substitute the null-string in place of newline
will give you
......
In Python v2 you can use the print()
function by including:
from __future__ import print_function
as the first statement in your source file.
As the print() docs state:
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Note, this is similar to a recent question I answered ( https://stackoverflow.com/a/12102758/1209279 ) that contains some additional information about the print()
function if you are curious.
Removing '\n' character from print statement
You can set the end
parameter of print
to be an empty string (or some other character):
print(details, file=userquiz, end='')
From the docs, you can see that it defaults to a newline:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
objects
to the text streamfile
, separated bysep
and followed byend
.sep
,end
,file
andflush
, if present, must be given
as keyword arguments.
This is what is being printed to the file currently.
How to Print in Python 2.7 without newline without buffering
Include this at the beginning of your file:
from __future__ import print_function
Then you can use both end
and flush
named parameters as if you were on Python 3. It seens you are missing the flush
parameter:
print("We're doing something...",end='', flush=True)
If you can't or would not like to do that for some reason, you should end your legacy print
statement with a sole comma. If you need the partial line to be printed, then you have to manually call sys.stdout.flush()
soon after printing:
print "We're doing something...",
sys.stdout.flush()
...
print "Done!"
How do I keep Python print from adding newlines or spaces?
import sys
sys.stdout.write('h')
sys.stdout.flush()
sys.stdout.write('m')
sys.stdout.flush()
You need to call sys.stdout.flush()
because otherwise it will hold the text in a buffer and you won't see it.
Related Topics
Selenium Element Not Visible Exception
Check If a Process Is Running or Not on Windows
How to Bind a List to a Parameter in a Custom Query in SQLalchemy
How to Mark a Portion of a Text Widget as Readonly
Where to Put Freeze_Support() in a Python Script
Regex Error - Nothing to Repeat
Cast Base Class to Derived Class Python (Or More Pythonic Way of Extending Classes)
How to Make a 4D Plot with Matplotlib Using Arbitrary Data
Websocket VS Rest API for Real Time Data
Matplotlib Log Scale Tick Label Number Formatting
Split a List into Parts Based on a Set of Indexes in Python
Multiple Inputs and Outputs in Python Subprocess Communicate
Subprocess.Call() Arguments Ignored When Using Shell=True W/ List
How to Close a Thread When Multithreading
How to Break a Long Line to Multiple Lines in Python