[Solved] How can i convert a string to tuple in python


Use ast.literal_eval to convert the string to a tuple.

>>> s = "('Who is Shaka Khan?',{'entities': [(7, 17, 'PERSON')]})," 
>>> import ast
>>> t = ast.literal_eval(s)
>>> t[0]
('Who is Shaka Khan?', {'entities': [(7, 17, 'PERSON')]})
>>> t[0][0]
'Who is Shaka Khan?'
>>> t[0][1]
{'entities': [(7, 17, 'PERSON')]}

Optionally, you can convert it to a dict for easy access

>>> d = dict(ast.literal_eval(s))
>>> d['Who is Shaka Khan?']
{'entities': [(7, 17, 'PERSON')]}

solved How can i convert a string to tuple in python