【问题标题】:Printing a string with the parentheses attched [closed]打印带有括号的字符串[关闭]
【发布时间】:2014-11-23 00:36:27
【问题描述】:

我正在尝试打印一个带有引号的字符串。我在这里使用交互模式。所以,我试图找出除了:

之外还有其他方法可以做到这一点:
print '"hello"'

或:

print "'hello'"

有没有办法打印带有引号的字符串?例如,如果我的字符串是"Python",则结果将是"Python" 而不是Python,没有附加括号。

【问题讨论】:

  • 你的意思是你想要字符串representation?您可以将某些内容粘贴回 Python 解释器并让它将其视为字符串文字?

标签: python string


【解决方案1】:

听起来您正在寻找repr() function

>>> s = 'hello'
>>> print repr(s)
'hello'

【讨论】:

    【解决方案2】:

    在交互模式下,这正是字符串的打印方式:

    >>> 'hello'
    'hello'
    

    【讨论】:

      猜你喜欢
      • 2023-02-15
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2013-06-14
      相关资源
      最近更新 更多