在引号内使用引号

新手上路,请多包涵

当我想在 Python 中执行 print 命令并且需要使用引号时,我不知道如何在不关闭字符串的情况下执行此操作。

例如:

 print " "a word that needs quotation marks" "

但是当我尝试执行上面的操作时,我最终关闭了字符串并且无法将我需要的单词放在引号之间。

我怎样才能做到这一点?

原文由 Thi G. 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 265
2 个回答

您可以通过以下三种方式之一执行此操作:

  1. 一起使用单引号和双引号:
    print('"A word that needs quotation marks"')
   "A word that needs quotation marks"

  1. 转义字符串中的双引号:
    print("\"A word that needs quotation marks\"")
   "A word that needs quotation marks"

  1. 使用三引号字符串:
    print(""" "A word that needs quotation marks" """)
   "A word that needs quotation marks"

原文由 Jamie Forrest 发布,翻译遵循 CC BY-SA 4.0 许可协议

你需要逃避它。 (使用 Python 3 打印功能):

 >>> print("The boy said \"Hello!\" to the girl")
The boy said "Hello!" to the girl
>>> print('Her name\'s Jenny.')
Her name's Jenny.

请参阅 python 页面以了解 字符串文字

原文由 Jonathon Reinhart 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题