I tried doing:
JavaScript
x
3
1
str = ""
2
"".join(map(str, items))
3
but it says str object is not callable. Is this doable using a single line?
Advertisement
Answer
Use string join()
method.
List:
JavaScript
1
5
1
>>> l = ["a", "b", "c"]
2
>>> " ".join(l)
3
'a b c'
4
>>>
5
Tuple:
JavaScript
1
5
1
>>> t = ("a", "b", "c")
2
>>> " ".join(t)
3
'a b c'
4
>>>
5
Non-string objects:
JavaScript
1
7
1
>>> l = [1,2,3]
2
>>> " ".join([str(i) for i in l])
3
'1 2 3'
4
>>> " ".join(map(str, l))
5
'1 2 3'
6
>>>
7