Python字符串文字和转义序列

时间:2012-03-21 18:34:35

标签: python

我有一个字符串

'“不,谢谢,妈妈,”我说,“我不知道需要多长时间。”

我将它分配给变量foobar

>>> foobar = """'"No, thanks, Mom," I said, "I don't know how long it will take."'"""

但是当我打印时,我得到了

>>> foobar
'\'"No, thanks, Mom," I said, "I don\'t know how long it will take."\''
>>>

如何准确打印与我指定的字符串相同的值?

3 个答案:

答案 0 :(得分:2)

要打印字符串,请使用... print

>>> foobar = """'"No, thanks, Mom," I said, "I don't know how long it will take."'"""
>>> print(foobar)
'"No, thanks, Mom," I said, "I don't know how long it will take."'

如果您只输入foobar,Python会打印foobar的representation。该表示被设计为有效的Python代码和/或用于调试,并不意味着输出给用户。

答案 1 :(得分:1)

>>> print foobar

foobar本身在字符串上调用repr,该字符串使用escapes打印字符串。 print foobar会在字符串上调用str

答案 2 :(得分:0)

打印相同的值。它只显示转义的单引号,以区别于cmd行用于显示字符串的单引号。