将str列表转换为单个大字符串

时间:2014-03-09 19:29:10

标签: string list python-3.x

我有一个str列表,我想将其转换为单个字符串

      poem = ['The time is here, the Captain said\n',
     'To talk about various things: boots - and planes - and wax,\n',
     'Of carrots; and queens.\n'
     'And why the sea is blue;\n'
     'and whether pigs can fly.\n']

我需要将此列表转换为一个大字符串

我试过这种方法:

for words in poem:
    stripped = word.strip('\n')

我这样做是否正确?

1 个答案:

答案 0 :(得分:2)

您可以先使用join(),然后将"\n"替换为空格:

whole_string = ''.join(poem).replace('\n, ' ')
print whole_string

<强>输出:

The time is here, the Captain said To talk about various things: boots - and planes - and wax, Of carrots; and queens. And why the sea is blue; and whether pigs can fly.