简单数据类型之间的转换
1.字符串如果是数字的,转换为int类型
a = "10" a = int(a) print(a)
2.数字类型转换成bool类型
a = 10 a = bool(a) print(a)
只有0才是false,其他值是True
复杂数据类型之间的转换
list:列表 tuple:元组 set 集合 dict:字典
4.列表类型转换为元组
name_list = ['a','b','c']
name_tuple = tuple(name_list)
5.列表转换为字典
name_list = ['a','b','c']
name_dict = dict.fromkeys(name_list, 默认值)
6.字典转换为列表
name_dict = {}
name_list = list(name_dict)
7.字符串转换为列表
会把字符串中每一个字符当做列表元素
name_str = "laosunaiyouyong"
name_list = list(name_str)
print(name_list)
8.通过字符串中存在的某个字符切割,转换为列表
name_str = 'sun jia nan'
name_list = name_str.strip().split(" ")
print(name_list)
9.列表通过某个字符拼接起来
name_list = ['sun','jia','nan']
name_str = " ".join(name_list)
print(name_str)