How do I run Python code with two print statements on a single line? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

5th Aug 2018, 4:46 AM
Cypher45
Cypher45 - avatar
8 Answers
+ 6
print("The number is", y) or print("The number is", end=" ") print(y) or print("The number is {}".format(y))
5th Aug 2018, 4:50 AM
Matthias
Matthias - avatar
+ 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 🙂
5th Aug 2018, 5:15 PM
David Ashton
David Ashton - avatar
+ 4
print(f"The number is {y}") also works
5th Aug 2018, 7:03 AM
David Ashton
David Ashton - avatar
+ 3
Matthias, oh your answer is right.
5th Aug 2018, 7:05 AM
Maninder $ingh
Maninder $ingh - avatar
+ 1
Maninder Singh No, this does not give the wanted output. Read his post again.
5th Aug 2018, 5:28 AM
Matthias
Matthias - avatar
0
print("The number is",y)
7th Aug 2018, 11:12 AM
kaushik rengan
kaushik rengan - avatar
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
7th Aug 2018, 2:26 PM
m.najmul hayat
m.najmul hayat - avatar