【问题标题】:Python list of strings to 1 string separated by comma & quotes (, & ")Python 字符串列表到 1 个字符串,用逗号和引号 (, & ") 分隔
【发布时间】:2018-11-21 14:14:19
【问题描述】:

我有一个字符串列表: list_of_strings = ["A", "List", "Of", "Strings"]

我想把它转换成一个新的字符串,看起来像:

new_string=""" "A", "List", "Of", "Strings" """

( 我尝试使用 join 功能,但我没有设法正确地做到这一点: ','.join(list_of_strings))

【问题讨论】:

  • 我认为这里有一个 XY 问题。你想通过构建这个字符串来达到什么目的?

标签: python string list join quotes


【解决方案1】:

你的字符串需要双引号。

new_string = "  " + ", ".join(['"{}"'.format(x) for x in list_of_strings]) + "  "

【讨论】:

  • 因为字符串前后没有空格? @timgeb
  • 是的。会解决的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-04-10
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多