-1

I have two variables say a=5 and b=10. I want to print them in new lines.

I tried print (a \n b) print(a '\n' b) print(a, '\n' b) print (a + '\n' +b). None of them works.

I can do print("a \n b") if a and b are strings.
For integers, is printing like print(str(a)+"\n"+str(b)) the only way?
I guess there must be another decent way which I don't know.

ram_23
  • 79
  • 11

2 Answers2

2

A simple solution:

print(a, b, sep='\n')
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
0

Just say print(*[a, b], sep="\n"). It unpacks a list (of n elements), and prints out every element. We specify a separator between the elements which in this case is newline or \n

a = 1
b = 2
print(*[a, b], sep="\n")
# output
1
2

You can use print(f"{a}\n{b}") as well in python 3.6+. Or print("{0}\n{1}".format(a, b)) in Python < 3.6

pissall
  • 7,109
  • 2
  • 25
  • 45
  • 1
    If you know there will be 2 items, there is no advantage of this over `print(a, b, sep="\n")` – Amadan Oct 08 '19 at 04:01
  • 1
    @ram_23 It unpacks a list (of n elements), and prints out every element. We specify a separator between the elements which in this case is `newline` or `\n`. – pissall Oct 08 '19 at 04:03