python3 list、tuple(元组)、str之间的相互转换

时间:2024-11-12 18:39:43
list()方法是把字符串str或元组转成数组
tuple()方法是把字符串str或数组转成元组
>>> s = "xxxxx"
>>> list(s)
['x', 'x', 'x', 'x', 'x']
>>> tuple(s)
('x', 'x', 'x', 'x', 'x')
>>> tuple(list(s))
('x', 'x', 'x', 'x', 'x')
>>> list(tuple(s))
['x', 'x', 'x', 'x', 'x']
列表和元组转换为字符串则必须依靠join函数
>>> "".join(tuple(s))
'xxxxx'
>>> "".join(list(s))
'xxxxx'
>>> str(tuple(s))
"('x', 'x', 'x', 'x', 'x')"#要是使用sublime text 3插件sublimeREPl,是不会显示外层的双引号的。上面同理。
>>> 

参考链接:

/sruru/article/details/7803208
/questions/5618878/how-to-convert-list-to-string
./2391349/568426
/questions/33054527/python-3-5-typeerror-a-bytes-like-object-is-required-not-str