-2

Trying to print

Numbers are: 1 2 3 4 5

from here

numbers = [1,2,3,4,5]
print("Numbers are: "),
for n in numbers:
    print(n),

But it doesn't print numbers in same line, it prints them on new line, why? I am on Python 3.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Lani
  • 3
  • 2

2 Answers2

1

In python 3.x you have to use print(..., end =" ") to substitute the new line with an empty space for example.

numbers = [1,2,3,4,5]
print("Numbers are: ", end =" ")
for n in numbers:
    print(n, end =" ")
Tina
  • 26
  • 4
  • Wow! This is working, why? Thank you for your help – Lani Apr 22 '19 at 12:54
  • Hi, this is a duplicate as @jonrsharpe has found out. Please try not to answer if it is a duplicate (and check for duplicates before answering) – godot Apr 22 '19 at 12:54
0

use :

print(n, end=" ")

because:

Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character.

ashishmishra
  • 363
  • 2
  • 14