1

For example, I have two variables int1 = 5 and int2 = 3 How can I print both the integers in separate lines using only a single print without typecasting to str. (like the following in C++: cout<<int1<<endl<<int2;)

Georgy
  • 12,464
  • 7
  • 65
  • 73
Fakru
  • 71
  • 1
  • 4
  • 9
  • 1
    @inspectorG4dget I think the duplicate target is a bit different. There are no answers there mentioning `sep='\n'`. Voted to reopen. – Georgy Oct 08 '19 at 07:57

3 Answers3

14

In python3:

print(string1, string2, sep='\n')

In python2:

print string1 + '\n' + string2

... or from __future__ import print_function and use python3's print

Since my first answer, OP has edited the question with a variable type change. Updating answer for the updated question:

If you have some integers, namely int1 and int2:

Python 3:

print(int1, int2, sep='\n')

Python 2:

print str(int1) + '\n' + str(int2)

or

from __future__ import print_function

print(int1, int2, sep='\n')

or

print '\n'.join([str(i) for i in [int1, int2]])
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • 1
    or for python2: print("\n".join([string1, string2, string_n]) For not have to concatenate X "\n" – Wonka Jun 20 '17 at 14:59
1

You can use the new line escape character and concatenate it between the two strings.

print string1 + "\n" + string2
mrapaport
  • 11
  • 2
1
print(string1 + "\n" + string2)

However, if one of the variables is an integer, you must convert is to a string first. If so:

str(string1)