How to turn a string into a list in python?

前端 未结 2 1261
太阳男子
太阳男子 2021-01-22 03:16
l = \"[\'Hello\', \'my\', \'name\', \'is\', \'Apple\']\"
l1 = [\'Hello\', \'my\', \'name\', \'is\', \'Apple\']

type(l) returns str

2条回答
  •  借酒劲吻你
    2021-01-22 03:50

    ast.literal_eval is a nice approach. For those preferint string manipulation, another option is:

    l1 = [x[1:-1] for x in l[1:-1].split(', ')]
    

提交回复
热议问题