Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show the output of two print statements on the same line?

Tags:

python

I have 2 separate print statements:

print "123"

print "456"

How can i make these 2 print statement appear on the same line? Note i need to use 2 print statements

output:

123456

like image 960
hssss Avatar asked Dec 08 '10 18:12

hssss


People also ask

How do you print two statements on the same line?

To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3. With Python 3, you do have the added flexibility of changing the end argument to print on the same line.

How do I display output on the same line?

Print on same line with some sign between elementsThe end=”,” is used to print in the same line with a comma after each element. We can use some other sign such as '. ' or ';' inside the end parameter.

How do you print two results on the same line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call.

How do I print multiple variables in one line?

You can use the cat() function to easily print multiple variables on the same line in R. This function uses the following basic syntax: cat(variable1, variable2, variable3, ...) The following examples show how to use this syntax in different scenarios.


2 Answers

In python 1.x and 2.x, a trailing comma will do what you want (with the caveat mentioned by others about the extra space inserted):

print "123",
print "456"

In python 3.x — or in python 2.6-2.7 with from __future__ import print_functionprint is a function and you should use end="" to force no extra ending character:

print("123", end="")
print("456")
like image 89
Andrew Jaffe Avatar answered Oct 21 '22 01:10

Andrew Jaffe


A comma after the string you are printing will suppress the newline. The \b is a special character that represents an ASCII backspace.

print '123',
print '\b456'
like image 33
Brent Newey Avatar answered Oct 21 '22 00:10

Brent Newey