Printing an int list in a single line python3

前端 未结 10 1298
臣服心动
臣服心动 2020-12-03 01:16

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

相关标签:
10条回答
  • 2020-12-03 01:44
    # Print In One Line Python
    
    print('Enter Value')
    
    n = int(input())
    
    print(*range(1, n+1), sep="")
    
    0 讨论(0)
  • 2020-12-03 01:47

    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 printed output, not a value that may be stored to a variable.

    print(*array, sep=' ')
    
    0 讨论(0)
  • 2020-12-03 01:48

    If you write

    a = [1, 2, 3, 4, 5]
    print(*a, sep = ',')
    

    You get this output: 1,2,3,4,5

    0 讨论(0)
  • 2020-12-03 01:51

    you can use more elements "end" in print:

    for iValue in arr:
       print(iValue, end = ", ");
    
    0 讨论(0)
提交回复
热议问题