【发布时间】:2016-08-06 02:57:19
【问题描述】:
我最近开始学习 Python,我希望你能帮助我解决一个困扰我的问题。我一直在使用Learn Python The Hard Way 在线学习 Python。在练习 6 中,我遇到了一个问题,我在使用 %r 字符串格式化操作时会产生两个不同的字符串。当我打印一个字符串时,我得到了带有单引号的字符串 (' ')。另一个我得到双引号(" ")。
代码如下:
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print "I said: %r." % x
print "I also said: %r." % y
第一个打印语句的结果:
I said: 'There are 10 types of people.'.
第二个打印语句的结果:
I also said: "Those who know binary and those who don't.".
我想知道为什么其中一个语句的结果带有单引号 (' ') 而另一个带有 (" ")。
]
附:我正在使用 Python 2.7。
【问题讨论】:
标签: python string printing string-formatting