-2

How do I turn these numbers into a list using python?

16 3 2 13 -> ["16","3","2","13"]

  • Please [edit](https://stackoverflow.com/posts/66166103/edit) your question to show what research you've done and any attempts you've made based on that research. Take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/questions/how-to-ask) for more tips about this site and asking questions. – im_baby Feb 12 '21 at 03:52
  • Does this answer your question? [String to list in Python](https://stackoverflow.com/questions/5453026/string-to-list-in-python) – DarrylG Feb 12 '21 at 03:54
  • What format does `16 3 2 13` represent? Did you mean to type `"16 3 2 13"` to indicate a single string? – Samwise Feb 12 '21 at 03:54

3 Answers3

1

You can divide it by using split:

"16 3 2 13".split()

Output:

["16","3","2","13"]
1

If you just want to split by whitespaces.

data = '16 3 2 13'
print(data.split())
Lohith
  • 866
  • 1
  • 9
  • 25
0
a = '16 3 2 13'

b = ['']
print(type(b))
print(len(b))

j = 0
for i in range(len(a)):
    if a[i] != ' ':
        b[j] = b[j] + a[i]
    else:
        j = j+1
        b.append('')

print(b)