rectangletangle
rectangletangle

Reputation: 52941

Converting strings into another data type, Python

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.

Upvotes: 0

Views: 397

Answers (1)

GWW
GWW

Reputation: 44093

You can use ast.literal_eval

>>> import ast
>>> ast.literal_eval('(0,0,0)')
(0, 0, 0)

Upvotes: 5

Related Questions