先看一下代码,ex6.py
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
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
你会看到下面结果
总结:1、Python可以通过文本里的双引号 " 或者单引号 ' 识别出字符串来。
2、你只要将格式化的变量放到字符串中,再紧跟着一个百分号 %,再紧跟着变量名,如果多个变量名,需要用小括号括起来,变量间用逗号分隔
3、注意用%r格式化输出字符串时,会把单引号打印出来