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)
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)
+ 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.
+ 3
g = 5
h = 6
print(g + "\n" + g)
I think this is right. By the way, what language is this?
+ 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".
+ 1
OK. it is python so the code i wrote before is right.
+ 1
Oops. I misunderstood it.
0
+ is an addition operator I want to print the values of both the variables in two separate line
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