Python - Combine all items in a list into a string
本问题已经有最佳答案,请猛点这里访问。
是否有方法将列表中的所有项组合成一个字符串?
1 | print(isbnl)# str(isbnl).strip('[]')) |
您有两种选择:
如果列表中的项目已经是字符串:
1 2 3 | list_of_strings = ['abc', 'def', 'ghi'] # give a list of strings new_string ="".join(list_of_strings) # join them into a new string |
如果列表中的项目不是所有字符串,则必须先将其转换为字符串:
1 2 3 4 5 6 | 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 |
现在,您可以根据需要打印或操作