How to split integer input in python?

后端 未结 3 854
走了就别回头了
走了就别回头了 2021-01-26 00:41

If you write like

n = str(input())

n = n.split()

print(n)

That will work. But if you try to do it with integers, you will get



        
3条回答
  •  后悔当初
    2021-01-26 01:01

    Do you want to separate several numbers? 1 2 3 -> [1, 2, 3]

    n = str(input())
    n = n.split()
    numbers = [int(i) for i in n]
    print(numbers)
    

    Or split a number in numeral? 123 -> [1, 2, 3]

    n = str(input())
    numbers = [int(i) for i in n]
    print(numbers)
    

    Use Nikhil answer, if you want to split a number with delimiters 1%3 -> [1, 3]

提交回复
热议问题