import string #字符串转list str = 'abcde' list = list(str) #list转字符串 str_convert = ''.join(list) #字符串转set set=set(str) #set转字符串 str=''.join(set) #元组转字符串 ''.join(tuple) #2、元组 tup=(1, 2, 3, 4, 5) #元组转为字符串,返回:(1, 2, 3, 4, 5) print tup.__str__() #元组转为列表,返回:[1, 2, 3, 4, 5] print list(tup) #元组不可以转为字典 #3、列表 nums=[1, 3, 5, 7, 8, 13, 20]; #列表转为字符串,返回:[1, 3, 5, 7, 8, 13, 20] print str(nums) #列表转为元组,返回:(1, 3, 5, 7, 8, 13, 20) print tuple(nums) #4、字符串 #字符串转为元组,返回:(1, 2, 3) print tuple(eval("(1,2,3)")) #字符串转为列表,返回:[1, 2, 3] print list(eval("(1,2,3)")) #字符串转为字典,返回:<type 'dict'> print type(eval("{'name':'ljq', 'age':24}"))
python学习笔记15-字符串 lsit set truple之间的相互转换
猜你喜欢
转载自www.cnblogs.com/zhwforever/p/10686131.html
今日推荐
周排行