我有一个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')
我这样做是否正确?
答案 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.