python list转str类型,str转list类型
- str 转 list类型:list(str)
str = "abc" list = list(str) print('str转list类型:',list) 打印结果:str转list类型: ['a', 'b', 'c']
- list 转 str类型:“”.join(list)
list = ['a','b','c'] str = "".join(list) print('list转str类型:',str) 打印结果:list转str类型: abc
注:如果list中是整型,需要先转为str,再使用join,否则会报错
list1 = [1,2,3]
str1 = "".join([str(x) for x in list1])
print("list中元素为整型,需要先转为str:",str1)
打印结果:list中元素为整型,需要先转为str: 123