How to convert string into tuple in python

(*-*)浩
Release: 2019-08-02 11:21:11
Original
17616 people have browsed it

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
Copy after login

Code uses tuple function:

r='(23,5,6)'
temp=r.replace('(','').replace(')','')
a=tuple([int(i) for i in temp.split(',')])
print a
Copy after login

Result:

#字符串变成了元组
(25,5,6)
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!