1

It's really odd but I have this string:

"['please', 'help']"

I want something that would get one argument at a time. I've searched everywhere for this but I didn't find anything.

Thanks in advance

Stam Kaly
  • 668
  • 1
  • 11
  • 26

2 Answers2

4

While eval is a correct approach, it can often have negative consequences. I suggest using ast.literal_eval, which is a more safe approach (as mentioned by the linked docs):

import ast
s = "['please', 'help']"
s_list = ast.literal_eval(s)
print s_list
Community
  • 1
  • 1
TerryA
  • 58,805
  • 11
  • 114
  • 143
3

Are you looking for something like this?

string = "['please', 'help']"
string_list = eval(string)

print string_list[0], string_list[1]

Edit: you should ideally use ast.literal_eval as the other answer suggests, if you are unsure of what the string contains.

Sreyantha Chary
  • 656
  • 4
  • 13