Home >Backend Development >Python Tutorial >How to convert string into tuple in python

How to convert string into tuple in python

(*-*)浩
(*-*)浩Original
2019-08-02 11:21:1117689browse

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:

How to convert string into tuple in python

##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)
For more Python-related technical articles, please visit the

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn