convert a string to tuple

Steven Bethard steven.bethard at gmail.com
Tue May 31 16:47:41 EDT 2005


querypk at gmail.com wrote:
> b is a string  b = '(1,2,3,4)' to b = (1,2,3,4)

py> tuple(int(s) for s in '(1,2,3,4)'[1:-1].split(','))
(1, 2, 3, 4)

Or if you're feeling daring:

py> eval('(1,2,3,4)', dict(__builtins__=None))
(1, 2, 3, 4)

Python makes no guarantees about the security of this second one though.

STeVe



More information about the Python-list mailing list