I\'m new to python and I\'m trying to scan multiple numbers separated by spaces (let\'s assume \'1 2 3\' as an example) in a single line and add it to a list of int. I did i
# Print In One Line Python
print('Enter Value')
n = int(input())
print(*range(1, n+1), sep="")
You have multiple options, each with different general use cases.
The first would be to use a for
loop, as you described, but in the following way.
for value in array:
print(value, end=' ')
You could also use str.join
for a simple, readable one-liner using comprehension. This method would be good for storing this value to a variable.
print(' '.join(str(value) for value in array))
My favorite method, however, would be to pass array
as *args
, with a sep
of ' '
. Note, however, that this method will only produce a print
ed output, not a value that may be stored to a variable.
print(*array, sep=' ')
If you write
a = [1, 2, 3, 4, 5]
print(*a, sep = ',')
You get this output: 1,2,3,4,5
you can use more elements "end" in print:
for iValue in arr:
print(iValue, end = ", ");