0

It is a program to take input from user and print that iput in one line. when the user gives input 1,2,3,4 the output that is every number come out in different line.

that is after each iteration of loop output comes in new line but i want output to be in single line.How can i do it? but i want output 1 2 3 4 5 how to get that output

print("Program to print a 1d array")
array=[]
i=0
j=0
while(i<5):               ##whileloop1
    item=int(input())
    array.append(item)
    i=i+1
while(j<5):              ##whileloop2
    print(array[j])
    j=j+1
Shubham Sharma
  • 1,753
  • 15
  • 24

3 Answers3

4

You can use join:

print("Program to print a 1d array")
array = [int(input()) for _ in range(5)]
print(' '.join(map(str, array)))
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

As @Bo102010 said, "In Python 3: print(array[j], end=' ')". But alternatively python supports printing lists as is. print(list) ,or in you case, print(array) will print out an array on one line.

JGerulskis
  • 800
  • 2
  • 10
  • 24
-1
>>> print(*array)
42 23 -17 0 99999
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
  • Why the downvote? Aren't we allowed to offer perfectly valid better solutions to the problem the OP actually is trying to solve? If *I* were asking such a question, I'd be happy to be told I'm doing it wrong and be shown a better way. – Stefan Pochmann May 24 '15 at 16:16