In python, how can I use regex to replace square bracket with parentheses

python, regex

Solution

If you do indeed have a list, then:

>>> s  = [1,2,3]
>>> str(tuple(s))
'(1, 2, 3)'

Problem

I have a list :`list = [1,2,3]`. And I would like to convert that into a string with parentheses: `string = (1,2,3)`. Currently I am using string replace `string = str(list).replace('[','(').replace(']',')')`. But I think there is a better way using regex.sub. But I have no idea how to do it. Thanks a lot

Original source