# 序列类型间的相互转换
l1 = ['a', 'b', 'c']
t1 = ('d', 'e', 'f')
s1 = "abc123"
s2 = "abc,123"
r1 = range(1, 4)
# list()函数:将其他函数转为列表
print(list(t1)) # 输出:['d', 'e', 'f']
print(list(s1)) # 输出:['a', 'b', 'c', '1', '2', '3']
print(s2.split(",")) # 输出:['abc', '123']
print(list(r1)) # 输出:[1, 2, 3]
# tuple()函数:将其他函数转换为元组
print(tuple(l1)) # 输出:('a', 'b', 'c')
print(tuple(s1)) # 输出:('a', 'b', 'c', '1', '2', '3')
print(tuple(s2.split(","))) # 输出:('abc', '123') 若是直接转换会将“,”也分割进去,所以要以list为桥梁
print(tuple(r1)) # 输出:(1, 2, 3)
# str()函数用于将单个数据转化为字符串,join()对列表进行连接
print(str(l1)) # 输出:['a', 'b', 'c']
# join要求所有元素必须是字符串,不能包含数字
print("".join(l1)) # 输出:abc
print(",".join(l1)) # 输出:a,b,c
print("|".join(t1)) # 输出:d|e|f
# print(",".join(r1)) # 报错:TypeError: sequence item 0: expected str instance, int found
s3 = "" # 将包含数字的序列输出
for i in r1:
s3 += str(i)
print(s3)