How to Convert List to String [Duplicate]

How to Convert List to String [Duplicate]

How can I convert a list to a string using Python?

3

3 Answers

Use ''.join:

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

If the list contains integers, convert the elements to string before joining them:

xs = [1, 2, 3]
s = ''.join(str(x) for x in xs)
6
>>> xs = [1, 2, 3]       
>>> " ".join(str(x) for x in xs)
'1 2 3'
1
xs = ['L', 'O', 'L']
lol_string = ''.join(map(str, xs))
3
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.