0
if operation == "+":
    print("{} + {} = ".format(number_1, number_2))
    print(number_1 + number_2)

How do I get the print(number_1 + number_2) to the same line of print("{} + {} = ".format(number_1, number_2))?

Martin
  • 7
  • 3
  • 1
    Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Mark Ormesher Jan 13 '19 at 16:57

5 Answers5

7

The easiest way is just to use the same call to print:

if operation == "+":
    print("{} + {} =".format(number_1, number_2), number_1 + number_2)

Another option (probably the best) is to extend your format string:

if operation == "+":
    print("{} + {} = {}".format(number_1, number_2, number_1 + number_2))

But you can also suppress the newline character that print puts at the end of the line by default:

if operation == "+":
    print("{} + {} = ".format(number_1, number_2), end="")
    print(number_1 + number_2)

Final answer:

if operation == "+":
    print(number_1, "+", number_2, "=", number_1 + number_2)

All of these versions print the same thing.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
1

You can change the line end element of the print statement in the following way:

if operation == "+":
    print("{} + {} = ".format(number_1, number_2),end = '')
    print(number_1 + number_2)
Sayok Majumder
  • 1,012
  • 13
  • 28
1
print(f"{number_1} + {number_2} = {number_1 + number_2}")

Python 3.6^ is required to use f string, its cool and nice, just an alternative to .format() method

0

if you need two print statements, use:

print("{} + {} = ".format(number_1, number_2), end=' ')
print(number_1 + number_2)

end prints end of line by deafult, but I changed it in here to print a space instead.

Andrii Chumakov
  • 279
  • 3
  • 12
0

This is the easiest way to use;

if operation == "+":
    print("{} + {} = ".format(number_1, number_2),number_1 + number_2)

or

print(f"{number_1} + {number_2} = {number_1 + number_2}")
direwolf
  • 73
  • 5