-6

So basically I want to convert a string which has multiple numbers to seperate integers in a list.

lst = []
s = '12 14 17'

Basically what I am trying to do is get the lst to be lst = [12,14,17] but I am having difficulty doing this since the string is one whole string and not one string per number.

Jack Jone
  • 21
  • 2
  • 1
    What have you tried so far? – Russ J Mar 06 '21 at 03:49
  • I have like I have stated tried to make the string which I have into something like s = '12' g = '14' and h = '17' so that I have one string per number where I can just do an str(s), str(g) and an str(h) and append them all to lst. But this is obviously a repetitive and difficult task to do if I have many numbers in the string. – Jack Jone Mar 06 '21 at 03:54
  • Just split it and convert to an integer: `[int(n) for n in s.split()]` and please consider searching before asking a new question. This has been answered many times. – Mark Mar 06 '21 at 04:02

1 Answers1

-1

You can split the string , then convert each elements to int -


>>> s = '12 14 17'

>>> list(map(int,s.split()))
[12, 14, 17]
>>> 

Vaebhav
  • 3,661
  • 1
  • 11
  • 25