convert a string to tuple

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue May 31 16:50:34 EDT 2005


In <1117570449.887422.225670 at g43g2000cwa.googlegroups.com>, querypk wrote:

> how do I convert 
> b is a string  b = '(1,2,3,4)' to b = (1,2,3,4)

In [1]: b = '(1,2,3,4)'

In [2]: b[1:-1]
Out[2]: '1,2,3,4'

In [3]: b[1:-1].split(',')
Out[3]: ['1', '2', '3', '4']

In [4]: tuple(b[1:-1].split(','))
Out[4]: ('1', '2', '3', '4')

Ooops, you wanted ints in there:

In [5]: tuple(map(int, b[1:-1].split(',')))
Out[5]: (1, 2, 3, 4)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list