这一节主要是对之前的print输出进行综合,分析教材中代码的知识点。
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.格式化字符串
上一节的格式化字符串
#2.字符串相加
上一节的字符串操作
#3.转义字符%r
上一节的字符串操作
#4.逗号的作用
之前说过,print语句结尾会自动输出一个换行符,两个print语句会分两行输出。这里逗号的作用相当于去掉这个换行符,让屏幕接着上一句末尾继续输出。
#5.字符序列乘以整数
字符序列可以乘以整型数字,实现复制输出。但不能乘以非整型数字,如将代码中的10改成浮点型10.0,将出现异常:
Traceback (most recent call last): File "ex7.py", line 6, inprint "." * 10.0
TypeError: can't multiply sequence by non-int of type 'float'
网友评论