This article mainly introduces the methods for conversion between Python strings, tuples, lists, and dictionaries. If you want to know more about Python strings, you can directly paste the code, sorry for the poor code writing.
#-*-Coding: UTF-8-*-#1. dictionary dict = {'name': 'zara ', 'age': 7, 'class ': 'first'} # convert the dictionary into a string and return:
{'Age': 7, 'name': 'zara ', 'class': 'first'} print type (str (dict), str (dict) # The dictionary can be converted into tuples, and the returned values are: ('age', 'name', 'class') print tuple (dict) # The dictionary can be converted into tuples. The returned values are: (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:
Print type (eval ("{'name': 'ljq', 'age': 24 }"))