要将 Python3 中的列表转换为字符串,你可以使用 join()
函数。例如:
>>> numbers = [1, 2, 3, 4]
>>> ' '.join(str(n) for n in numbers)
'1 2 3 4'
这将创建一个字符串,其中数字列表中的每个数字都是字符串,并以空格分隔。你也可以使用其他字符作为分隔符,例如:
>>>','.join(str(n) for n in numbers)
'1,2,3,4'
你还可以使用 join()
函数来将字符串列表转换为字符串:
>>> words = ['cat', 'window', 'defenestrate']
>>> ' '.join(words)
'cat window defenestrate'
如果你想要在列表中的每个元素之间插入换行符,可以使用 \n
作为分隔符:
>>>'\n'.join(words)
'cat\nwindow\ndefenestrate'
希望这能帮到你!