代码如下:
1 # coding : utf-8 2 x = "There are %d types of people." % 10 3 binary = "binary" 4 do_not = "don't" 5 y = "Those who know %s and those who %s." % (binary, do_not) 6 7 print x 8 print y 9 10 print "I said: %r." % x11 print "I also said: %s." % y #将原代码中的‘’单引号删去了,与%r的打印结果做对比12 13 hilarious = False14 joke_evaluation = "Isn't that joke so funny?! %r"15 16 print ( joke_evaluation % hilarious )17 18 w = "This is the left side of..."19 e = "a string with a right side."20 21 print w + e
运行结果:
%r是repr;%s是str;前者是被repr处理后的string对象,后者直接是string对象。 使用%r打印出的是字符串会带上引号‘’,使用%s则不会