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.
You can pass it to eval() (note: this is unsafe):
Just do:
<!-- language: python -->
strTup = '(5,6)'
eval(strTup)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With