0

I cannot seem to make a list from an input and not have that list separate any multi-digit numbers into individual numbers. e.g. 100 becomes 1, 0, 0, 284 becomes 2, 8, 4, and so on.

data = list(input("What is your data? must be numbers separated by spaces\n"))
print(data)

If I entered my data as: 1 2 3 100, it would print as 1, 2, 3, 1, 0, 0. Is there any way I can have it instead be 1, 2, 3, 100?

  • 1
    `input` returns a string. Calling `list` on a string converts that string to a list of single characters. The API you need is `.split()`. – Tim Roberts Oct 02 '22 at 00:59
  • Welcome to Stack Overflow. Please see the linked duplicate. For future reference, please read [ask] and [How much research is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592) – Karl Knechtel Oct 02 '22 at 01:08

2 Answers2

0

You could do this,

In [1]:  input("What is your data? must be numbers separated by spaces\n").split(' ')
What is your data? must be numbers separated by spaces
1 2 3 100
Out[1]: ['1', '2', '3', '100']
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
-1

You should split your input before putting it in a list.

And probably, you want to convert the strings to integers.

Something like:

s = input().split(" ") 
data = [int(x) for x in s] 
print(data) 
user107511
  • 772
  • 3
  • 23