1. 字符串轉(zhuǎn)列表
str1 ="hi hello world"
print(str1.split(""))
輸出:
['hi', 'hello', 'world']
2. 列表轉(zhuǎn)字符串
l = ["hi","hello","world"]
print("".join(l))
輸出:
hi hello world
# 字符轉(zhuǎn)數(shù)字
rmb = input('價(jià)格:’)
if rmb.isdigit():
? ? rmb = int(rmb)
字典dict = {'name': 'Zara', 'age': 7, 'class': 'First'}
#字典轉(zhuǎn)為字符串拿愧,返回:{'age': 7, 'name': 'Zara', 'class': 'First'}print type(str(dict)), str(dict)
#字典可以轉(zhuǎn)為元組矮燎,返回:('age', 'name', 'class')print tuple(dict)
#字典可以轉(zhuǎn)為元組,返回:(7, 'Zara', 'First')print tuple(dict.values())
#字典轉(zhuǎn)為列表蚣常,返回:['age', 'name', 'class']print list(dict)
#字典轉(zhuǎn)為列表print dict.values
#2、元組tup=(1, 2, 3, 4, 5)
#元組轉(zhuǎn)為字符串钞螟,返回:(1, 2, 3, 4, 5)print tup.__str__()
#元組轉(zhuǎn)為列表悔醋,返回:[1, 2, 3, 4, 5]print list(tup)
#元組不可以轉(zhuǎn)為字典
#3、列表nums=[1, 3, 5, 7, 8, 13, 20];
#列表轉(zhuǎn)為字符串谣妻,返回:[1, 3, 5, 7, 8, 13, 20]print str(nums)
#列表轉(zhuǎn)為元組萄喳,返回:(1, 3, 5, 7, 8, 13, 20)print tuple(nums)
#列表不可以轉(zhuǎn)為字典#4、字符串
#字符串轉(zhuǎn)為元組蹋半,返回:(1, 2, 3)print tuple(eval("(1,2,3)"))
#字符串轉(zhuǎn)為列表他巨,返回:[1, 2, 3]print list(eval("(1,2,3)"))
#字符串轉(zhuǎn)為字典,返回:print type(eval("{'name':'ljq', 'age':24}"))