這一節(jié)主要是對(duì)之前的print輸出進(jìn)行綜合移怯,分析教材中代碼的知識(shí)點(diǎn)。
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary,do_not) #1
print "I also said: '%s'." % y?#1
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,?#2
print end7 + end8 + end10 + end11 + end12?#2
formatter = "%r %r %r %r"
print formatter % (True,False,False,True)
print formatter % (formatter, formatter, formatter, formatter) #3
print formatter % (
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "I had this thing.", #4
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "That you could type up right.",?#4
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "But it didn't sing.", #4
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "So I said goodnight.")
print "." * 10 #5
#1.格式化字符串
上一節(jié)的格式化字符串
#2.字符串相加
上一節(jié)的字符串操作
#3.轉(zhuǎn)義字符%r
上一節(jié)的字符串操作
#4.逗號(hào)的作用
之前說(shuō)過(guò),print語(yǔ)句結(jié)尾會(huì)自動(dòng)輸出一個(gè)換行符,兩個(gè)print語(yǔ)句會(huì)分兩行輸出如暖。這里逗號(hào)的作用相當(dāng)于去掉這個(gè)換行符,讓屏幕接著上一句末尾繼續(xù)輸出忌堂。
#5.字符序列乘以整數(shù)
字符序列可以乘以整型數(shù)字盒至,實(shí)現(xiàn)復(fù)制輸出。但不能乘以非整型數(shù)字士修,如將代碼中的10改成浮點(diǎn)型10.0枷遂,將出現(xiàn)異常:
Traceback (most recent call last):? File "ex7.py", line 6, inprint "." * 10.0
TypeError: can't multiply sequence by non-int of type 'float'