代码如下:
# coding : utf-8
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 x
print y
print "I said: %r." % x
print "I also said: %s." % y #将原代码中的‘’单引号删去了,与%r的打印结果做对比
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print ( joke_evaluation % hilarious )
w = "This is the left side of..."
e = "a string with a right side."
print w + e
运行结果:
%r是repr搜索;%s是str;前者是被repr处理后的string对象,后者直接是string对象。
使用%r打印出的是字符串会带上引号‘’,使用%s则不会