如何使用单行在Python中将列表/元组转换为以空格分隔的字符串? [英] How to turn a list/tuple into a space separated string in python using a single line?
本文介绍了如何使用单行在Python中将列表/元组转换为以空格分隔的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试做:
str = ""
"".join(map(str, items))
,但它说str对象是不可调用的。
but it says str object is not callable. Is this doable using a single line?
推荐答案
使用字符串 join()
方法。
列表:
>>> l = ["a", "b", "c"]
>>> " ".join(l)
'a b c'
>>>
元组:
>>> t = ("a", "b", "c")
>>> " ".join(t)
'a b c'
>>>
非字符串对象:
>>> l = [1,2,3]
>>> " ".join([str(i) for i in l])
'1 2 3'
>>> " ".join(map(str, l))
'1 2 3'
>>>
这篇关于如何使用单行在Python中将列表/元组转换为以空格分隔的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文