Printing Without A Newline In Python3 In Python 3, print is a function that prints output on different lines, every time you use the function. However, you can avoid this by introducing the argument end and assigning an empty string to it. This will prevent the next output from being printed in a new line. You can see that on documentation (Built-in Functions): code print/code(.objects, sep=' ', end=' n', file=sys.stdout, flush=False) The end of the line on each print is added through the parameter end of print. By default, it’s “end=’ n’”, wher.
Active1 year, 10 months ago
This question already has an answer here:
- How to print without newline or space? 21 answers
I've started programming today and have this issue with Python. It's pretty dumb but I can't figure out how to do it. When I use the print command, it prints whatever I want and then goes to a different line. For example:
Should return:
this should be on the same line
but instead returns:
this should be
on the same line
on the same line
More precisely I was trying to create a program with
if
that told me whether a number was a 2 or notBut it doesn't recognise the last
(x)
as the value entered, and rather prints exactly: '(x)' (the letter with the brackets). To make it work I have to write:And if e.g. I enter
test2(3)
that gives:Nope, that is not a two, that is a
3
3
So either i need to make Python recognise my (x) inside a print line as the number; or to print two separate things but on the same line. Thanks in advance and sorry for such a stupid question.
IMPORTANT NOTE: I am using version 2.5.4
Another note: If i put
Ghost4Manprint 'Thing' , print 'Thing2'
it says 'Syntax error' on the 2nd print.73211 gold badge77 silver badges1616 bronze badges
JavicobosJavicobos
marked as duplicate by Baruch, Dirk, Ryan Bigg, falsetru, fedorquiAug 8 '13 at 9:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
5 Answers
In Python 3.x, you can use the
end
argument to the print()
function to prevent a newline character from being printed:In Python 2.x, you can use a trailing comma:
You don't need this to simply print a variable, though:
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 useto get access to the Python 3 print function or use
Sven MarnachSven Marnachsys.stdout.write()
.386k8585 gold badges779779 silver badges717717 bronze badges
In Python 2.x just put a
,
at the end of your print
statement. If you want to avoid the blank space that print
puts between items, use sys.stdout.write
.yields:
Note that there is no newline or blank space between the two strings.
In Python 3.x, with its print() function, you can just say
and get:
There is also a parameter called
sep
that you can set in print with Python 3.x to control how adjoining strings will be separated (or not depending on the value assigned to sep
)E.g.,
Python 2.x
gives
Python 3.x
gives
LevonLevon95.3k2525 gold badges168168 silver badges168168 bronze badges
If you're using Python 2.5, this won't work, but for people using 2.6 or 2.7, try
results in
For those using 3.x, this is already built-in.
Hugh BothwellHugh Bothwell45.9k66 gold badges6161 silver badges8787 bronze badges
You simply need to do:
However in:
There is implicit whitespace (ie
' '
).You also have the option of:
Jon Clements♦Jon Clements106k2121 gold badges192192 silver badges226226 bronze badges
Utilize a trailing comma to prevent a new line from being presented:
Should be:
In addition, you can just attach the variable being passed to the end of the desired string by:
You can also use:
You can access additional documentation regarding string formatting utilizing the
RobBRobB%
operator (modulo).8,06211 gold badge2121 silver badges3434 bronze badges
Not the answer you're looking for? Browse other questions tagged pythonprintingnewlinepython-2.5 or ask your own question.
- Related Questions & Answers
- Selected Reading
In python the print statement adds a new line character by default. So when we have multiple print statements the output from each of them is printed in multiple lines as you can see in the example below. Our goal is to print them in a single line and use some special parameters to the print function to achieve that.
Normal Print()
The below example prints multiple statements with new lines in each of them.
Example
Output
Running the above code gives us the following result −
Using end Parameter
We can use the end parameter to use a specific character at the end of the output from each print statement. In the below example we use ',' as special character with the end parameter, which will appear at the end of the output of each print statement. The result will not be printed in multiple lines.
Example
Output
Running the above code gives us the following result −