Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string tuple to a tuple [duplicate]

I have a user entered string which is already in tuple format and would like to convert/cast it to a actual tuple in python. How can I do this? E.g:

strTup = '(5, 6)'

Would like to convert the above to the tuple (5, 6). I tried tuple(strTup) which did not work as it made each character into its own tuple.

like image 614
oscilatorium Avatar asked Jul 29 '14 19:07

oscilatorium


Video Answer


1 Answers

You can pass it to eval() (note: this is unsafe):

Just do:

    <!-- language: python -->
    strTup = '(5,6)'
    eval(strTup)
like image 185
Luna Feng Avatar answered Oct 29 '22 14:10

Luna Feng