上一篇文章的結(jié)尾留了個(gè)問題嗽上,這課來解答愕提,答案就在書中的“常見問題回答”中:
%r與%s有什么不同
%r是用來做調(diào)試(debug)比較好呻惕,因?yàn)樗仫@示變量的原始數(shù)據(jù)专酗,而%s和其它的符號(hào)則是用來向客戶顯示輸出的
貼上ex6的練習(xí)代碼:
# -*- 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 #這里注意一下用逗號(hào)連接和加號(hào)連接的區(qū)別
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,#逗號(hào)可以讓后一print內(nèi)容與這行內(nèi)容連接在一起形成一行嬉橙,否則兩行
print end7 + end8 + end9 + end10 + end11 + end12