字符串、列表、元组、字典,四个互相组合,共12种情况。如下:
1.字符串转列表
运行代码:
s = 'My name is littlecurl'
strToList = list(s)
print(strToList)
运行结果:
['M', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'l', 'i', 't', 't', 'l', 'e', 'c', 'u', 'r', 'l']
2.字符串转元组
运行代码:
s = 'My name is littlecurl'
strToTuple = tuple(s)
print(strToTuple)
运行结果:
('M', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'l', 'i', 't', 't', 'l', 'e', 'c', 'u', 'r', 'l')
3.字符串转字典
运行代码:
s = "{'My name is ':'littlecurl'}"
strToDict = eval(s)
print(strToDict)
运行结果:
{'My name is ': 'littlecurl'}
注意:
看这里的字符串是特殊构造的,最外层的标点符号是双引号" ", 双引号内存是一个字典,字典内的符号是 单引号 ' '
然后运用的方法是 eval( ) &