+ 3
How do I run Python code with two print statements on a single line?
y=100 print("The number is ") print (y) OUTPUT ----------- The number is 100 My question is, what should I do to get the output "The number is 100" (integer 100 on same line as char "The number is") I'm sorry if what I said was difficult to understand, this is my first programming language.
8 Answers
+ 6
print("The number is", y)
or
print("The number is", end=" ")
print(y)
or
print("The number is {}".format(y))
+ 5
Cypher45 The default end command of the print() function is end="\n", i.e. a new line, so if you leave it out, a new line is printed, so
print("Hello")
print("World!")
outputs
Hello
World!
but
print("Hello", end=" ")
print("World!")
outputs
Hello World!
and
print("Hello", end="")
print("World!")
outputs
HelloWorld!
and
print("Hello", end="\n\n")
print("World!")
outputs
Hello
World!
Hope this helps.
PS there is also a separator command that defaults to a space, i.e. sep=" ", so
print("Hello", "World!")
outputs
Hello World!
but
print("Hello", "World!", sep=" there, ")
outputs
Hello there, World!
You can combine them too.
(It's better style to put sep before end but not strictly necessary.)
print("Hello", "World", sep=" there, ", end="")
print("!")
outputs
Hello there, World!
For an example, see lines 7 and 8:
https://code.sololearn.com/csN2U0KlfByd/?ref=app
đ
+ 4
print(f"The number is {y}") also works
+ 3
Matthias, oh your answer is right.
+ 1
Maninder Singh No, this does not give the wanted output. Read his post again.
0
print("The number is",y)
0
have a different between python 2 and python 3 :
1. if you used a python 2, just type :
print "hello, najmul"
2. and then if used python 3, must a type :
print ("hello, najmul")
i think like that