【问题标题】:Convert a list of str into a single huge string将 str 列表转换为单个大字符串
【发布时间】:2014-03-09 19:29:10
【问题描述】:

我有一个 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')

我做得对吗?

【问题讨论】:

    标签: string list python-3.x


    【解决方案1】:

    您可以先使用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. 
    

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      • 2013-06-02
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      相关资源
      最近更新 更多