There are many ways to convert strings into tuples in Python. You can directly eval(r), you can directly use exception conversion, or you can borrow list operations, etc. I will use one of them, combined with many to facilitate review of the usage of different data:
##The code uses the eval function: (recommended learning :Python video tutorial)
r='(23,5,6)' val = eval(r) print val
Code uses tuple function:
r='(23,5,6)' temp=r.replace('(','').replace(')','') a=tuple([int(i) for i in temp.split(',')]) print a
Result:
#字符串变成了元组 (25,5,6)
Python Tutorial column to learn!
The above is the detailed content of How to convert string into tuple in python. For more information, please follow other related articles on the PHP Chinese website!