Home > Software design >  how to print the output in multiple lines
how to print the output in multiple lines

Time:07-07

 var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29

print(100*(var1 var2))

in the above code in python, I am getting the output as a single-line output enter image description here

I want it to be printed in multiple lines. what should I do to make it work?. I have tried using the \n escape character but I am not getting the required output.

CodePudding user response:

Add \n character:

var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29

print(100*(var1 var2 '\n'))

CodePudding user response:

Based on the code mentioned by you, below is the output:

arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86

As per my understanding of the question, the output expected is as below:

arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86

To get the above output, please use "\n" in your code as shown below:

var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29

print(100*(var1 var2 '\n'))

Also, if you need "86" also to be printed in next line, you can use the below code and so on:

var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29

print(100*(var1 "\n" var2 '\n')) 

And expect below output:

arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86

CodePudding user response:

I'd suggest using a for loop rather than string multiplication if your intent is to print the same thing 100 times; it'll consume less memory (not that 100 times a 6-character string is a lot, but with a larger number it could become significant), and it makes the intent more obvious to the casual reader:

var1 = "arun"
var2 = "86"

for _ in range(100):
    print(var1 var2)

If you really wanted to construct a single string with newlines in it, apart from inserting \n before multiplying:

print(100*(var1 var2 '\n'))

another option would be to use str.join with list multiplication:

print('\n'.join([var1 var2]*100))

or a generator expression:

print('\n'.join(var1 var2 for _ in range(100)))

or to do either of the above with the sep parameter in print():

print(*(var1 var2 for _ in range(100)), sep='\n')
  • Related