/ Published in: Python
// Is there a way in python to convert a list say [1,2,3,4] to a string and
// also how to convert a string to a list?
// use str() or repr() to convert to a string.
// eval() will convert back to a list.
// also how to convert a string to a list?
// use str() or repr() to convert to a string.
// eval() will convert back to a list.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
x = range(10) print x # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print str(x) # '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]' print repr(x) # '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]' y = str(x) z = eval(y) print x == z # 1 print z # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]