0

I have the string "(0, 0, 0)". I'd like to be able to convert this to a tuple. The built in tuple function doesn't work for my purposes because it treats each character as an individual item. I want to be able to convert "(0, 0, 0)" to (0, 0, 0) programmatically.

1 Answer 1

5

You can use ast.literal_eval

>>> import ast
>>> ast.literal_eval('(0,0,0)')
(0, 0, 0)
Sign up to request clarification or add additional context in comments.

2 Comments

Or eval(string, {}, {}) in pre-2.6, where ast doesn't exist
Exactly what I was looking for, thanks! Also David Zaslavsky, in my case it's 2.7.2 so that doesn't matter. Good information, nonetheless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.