如何将列表转换为字符串

2022-09-05 01:07:01

如何使用Python将列表转换为字符串?


答案 1

用:''.join

xs = ['1', '2', '3']
s = ''.join(xs)

如果列表包含整数,请在联接元素之前将元素转换为字符串:

xs = [1, 2, 3]
s = ''.join(str(x) for x in xs)

答案 2
>>> xs = [1, 2, 3]       
>>> " ".join(str(x) for x in xs)
'1 2 3'