【问题标题】:How do I print a string in double quotes? (NOT PRINT DOUBLE QUOTES)如何在双引号中打印字符串? (不打印双引号)
【发布时间】:2022-01-13 15:51:48
【问题描述】:

要清楚,我不想自己打印双引号,我希望 python 用双引号而不是单引号打印出我的字符串?

例如,如果我在 python 中运行以下代码-

print(["this is a string!", "this is another string!"])

结果是

['this is a string!', 'this is another string!']

我希望输出也用双引号引起来。我该怎么做?

【问题讨论】:

标签: python string quotes


【解决方案1】:

使用json.dumps,这将组成一个完整的字符串,并且内部字符串将被双引号

v = ["this is a string!", "this is another string!"]

# type is list
print(v) # ['this is a string!', 'this is another string!']

# type is string
print(json.dumps(v)) # ["this is a string!", "this is another string!"]

【讨论】:

  • 这与 post 中复制的解决方案基本相同;我认为与其将解决方案复制到重复的问题,不如将其标记为重复并继续。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2015-11-17
  • 1970-01-01
  • 2016-07-17
  • 2020-03-01
相关资源
最近更新 更多