上一篇文章的结尾留了个问题,这课来解答,答案就在书中的“常见问题回答”中:
%r与%s有什么不同
%r是用来做调试(debug)比较好,因为它地显示变量的原始数据,而%s和其它的符号则是用来向客户显示输出的
贴上ex6的练习代码:
# -*- coding:utf-8 -*-
x = "There are %d types of people." % 10 #格式化字符串,有10种类型人
binary = "binary" #字符串赋值
do_not = "don't" #字符串赋值
y = "Those who know %s and those who %s." % (binary,do_not) #格式化字符串
print x #输出x
print y #输出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 #这里注意一下用逗号连接和加号连接的区别
print w,e
ex7代码:
# -*- coding:utf-8 -*-
print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
print end1 + end2 + end3 + end4 + end5 + end6,#逗号可以让后一print内容与这行内容连接在一起形成一行,否则两行
print end7 + end8 + end9 + end10 + end11 + end12
网友评论