打印带有括号的字符串

时间:2014-09-28 14:41:04

标签: python string

我正在尝试打印带有引号的字符串。我在这里 NOT 使用交互模式。所以,我试图找出除了以外还有其他方法可以做到这一点:

print '"hello"'

或:

print "'hello'"

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

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

>>> 'hello'
'hello'