\n giving syntax error in python. I want to print the values of variables in two separate lines | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

\n giving syntax error in python. I want to print the values of variables in two separate lines

g=5 h=6 print(h\ng)

3rd Jun 2018, 11:17 AM
Sujith
8 Answers
+ 2
try this: a=1 b=2 c=3 print(a,"\n",b,"\n",c,sep="') # The default separator for function print is a whitespace (" ") , so you should reassign it to a enpty string or convert integers to strings first, then use + for string concatenation: a=1 b=2 c=3 print(str(a)+"\n"+str(b)+"\n"+str(c)) and of course there are many other ways such as string formatting: a=1 b=2 c=3 print("{}\n{}\n{}".format(a,b,c)) And the simplest way: a=1 b=2 c=3 print(a) print(b) print(c)
3rd Jun 2018, 6:00 PM
Ꝏē«‹å؁
+ 5
h and g are integers "\n" is a string. print (h, "\n", g) You can separate different types of values with a , in your print statement.
3rd Jun 2018, 11:52 AM
Paul
Paul - avatar
+ 3
g = 5 h = 6 print(g + "\n" + g) I think this is right. By the way, what language is this?
3rd Jun 2018, 11:21 AM
Disvolviĝo;
Disvolviĝo; - avatar
+ 2
You may mean this: g=5 h=6 print(g, h, sep="\n") or simply: g=5 h=6 print(g, "\n", h) The using of \n should be placed in a string (2 quotation marks) like "\n".
3rd Jun 2018, 12:52 PM
Ꝏē«‹å؁
+ 1
OK. it is python so the code i wrote before is right.
3rd Jun 2018, 11:30 AM
Disvolviĝo;
Disvolviĝo; - avatar
+ 1
Oops. I misunderstood it.
3rd Jun 2018, 11:56 AM
Disvolviĝo;
Disvolviĝo; - avatar
0
+ is an addition operator I want to print the values of both the variables in two separate line
3rd Jun 2018, 11:52 AM
Sujith
0
thank you all for the help.i have another minor doubt hope you guys can solve it easily. I have written a simple code below just like the one in the original post. I used /n inside the double quote and got the output as I wanted(value of each variable in separate line) but the values of second and third variable has an extra space compared to first one. any solution to overcome this minor problem. a=1 b=2 d=3 print(a,"\n",b,"\n",d,) ###output### 1 2 3 #extra space for 2nd and 3rd output
3rd Jun 2018, 5:41 PM
Sujith