python-字符串、列表、元祖之间的转换
作者:互联网
1.字符串转换成列表
s = 'erfwerg' v = list(s) print(v) # 输出结果:['e', 'r', 'f', 'w', 'e', 'r', 'g']
2.字符串转换成元祖
s = 'erfwerg' v = tuple(s) print(v) # 输出结果:('e', 'r', 'f', 'w', 'e', 'r', 'g')
3.列表转换成功字符串
# 列表中只有字符串 li = ['qqq', 'aaa', 'www', 'eee', 'ggg'] s = ''.join(li) print(s) # 输出结果:qqqaaawwweeeggg # 列表中既有数字,又有字符串 li = ['qqq', 'aaa', 555, 'eee', 'ggg', 111] s = '' for new_li in li: s = s + str(new_li) print(s) # 输出结果:qqqaaa555eeeggg111
4.列表转换成元祖
li = ['qqq', 'aaa', 666, 'eee', 'ggg'] v = tuple(li) print(v) # 输出结果:('qqq', 'aaa', 666, 'eee', 'ggg')
5.元祖转换成列表
tu = ('qqq', 'aaa', 666, 'eee', 'ggg') v = list(tu) print(v) # 输出结果:['qqq', 'aaa', 666, 'eee', 'ggg']
6.元祖转换成字符串
#元祖中只有字符串 tu = ('qqq', 'aaa', 'www', 'eee', 'ggg') v = ''.join(tu) print(v) # 输出结果:qqqaaawwweeeggg #元祖中既有数字,又有字符串 tu = ('qqq', 'aaa', 1111, 'eee', 'ggg') s='' for new_tu in tu: s=s+str(new_tu) print(s) # 输出结果:qqqaaa1111eeeggg
标签:python,tu,li,ggg,qqq,print,列表,元祖 来源: https://www.cnblogs.com/lijinping716/p/11297711.html