SDSDFsafad
SDSDFsafad

Reputation: 39

Make an input string into a list in python

I want to turn the string:

avengers,ironman,spiderman,hulk

into the list:

['avengers','ironman','spiderman','hulk']

I tried

list = raw_input("Enter string:")
list = list.split()

but it's not working the right way as I need it, it's taking the whole string and makes it as one item in the list (it works fine without the "," but that's not what I need)

Upvotes: 3

Views: 16559

Answers (2)

hft
hft

Reputation: 1245

Hello guys i want to make for example the next string:

avengers,ironman,spiderman,hulk into the next list:


['avengers','ironman','spiderman','hulk']

i tried that `

list = raw_input("Enter string:")
    list = list.split()

Do this instead:

list = raw_input("Enter string:")
list = list.split(",")

And, as mentioned by the others, you might want to not use the name "list" for your string/array.

Upvotes: 2

Tales Pádua
Tales Pádua

Reputation: 1461

If you dont pass anything to split method, it splits on empty space. Pass the comma as argument:

my_list.split(',')

edited so you dont use list as name

Upvotes: 8

Related Questions