Python - 将列表中的所有项组合成一个字符串

时间:2014-07-17 14:02:15

标签: python

有没有办法将列表中的所有项目合并为一个字符串?

    print(isbnl)# str(isbnl).strip('[]'))

1 个答案:

答案 0 :(得分:2)

您有两种选择:

如果列表中的项目已经是字符串:

list_of_strings = ['abc', 'def', 'ghi'] # give a list of strings

new_string = "".join(list_of_strings)   # join them into a new string

如果列表中的项目不是所有字符串,则必须先将它们转换为字符串:

list_of_nonstrings = [1.83, some_object, 4, 'abc'] # given a list of mixed types

# construct new list of strings for join function
list_of_strings = [str(thing) for thing in list_of_nonstrings]

new_string = "".join(list_0f_strings) # join into new string, just like before

现在您可以根据需要打印或操作new_string