Conversion of Python strings, tuples, lists, and dictionaries.

Source: Internet
Author: User

Conversion of Python strings, tuples, lists, and dictionaries.

I am not talking about this nonsense. I am posting Code directly to you. If the code is not well written, I 'd like to forgive you.

#-*-Coding: UTF-8-*-#1. dictionary dict = {'name': 'zara ', 'age': 7, 'class ': 'first'} # convert the dictionary into a string and return: <type 'str'> {'age': 7, 'name': 'zara ', 'class ': 'first'} print type (str (dict), str (dict) # The dictionary can be converted into a tuples, And the return value is: ('age', 'name', 'class ') print tuple (dict) # The dictionary can be converted into a tuples, and the returned result is: (7, 'zara ', 'First') print tuple (dict. values () # convert the dictionary to the list and return: ['age', 'name', 'class'] print list (dict) # convert the dictionary to the list print dict. values #2. tup = (1, 2, 3, 4, 5) # convert the tuples into strings and return print tup: (1, 2, 3, 4, 5. _ str _ () # convert the tuples to the list and return: [1, 2, 3, 4, 5] print list (tup) # The tuples cannot be converted into dictionaries #3. List nums = [1, 3, 5, 7, 8, 13, 20]; # convert the list into strings and return: [1, 3, 5, 7, 8, 13, 20] print str (nums) # convert the list into tuples and return the following values: (1, 3, 5, 7, 8, 13, 20) print tuple (nums) # The list cannot be converted into a dictionary #4. The string # is converted into a tuples and returns the following: (1, 2, 3) print tuple (eval ("(, 3) ") # convert string to list and return: [1, 2, 3] print list (eval (" (, 3) ") # convert string to dictionary and return: <type 'dict '> print type (eval ("{'name': 'ljq', 'age': 24 }"))

Articles you may be interested in:
  • Python converts a string into a dictionary dict
  • Examples of python list and tuples
  • Python tuples (Tuple)
  • Usage of tuples in basic python tutorials
  • Convert a dictionary to its json string in python
  • How to convert string and dictionary types in Python
  • Python entry list and metadata
  • List, Dictionary, tuples, and collection Data Structure in Python
  • Python list, dictionary, and string operations
  • Tuple tuples in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.