Entry
Converting strings to/from lists
Converting strings to/from lists
Jul 5th, 2000 09:58
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 7, Kevin F. Smith
"""
Packages: basic_datatypes.lists;basic_datatypes.strings
"""
"""
> 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.
"""
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]