跟着笨方法学python个教程学习,边学边做习题边记录。
新手学python-笨方法学python-ex8-打印打印 - 学习记录
跟着课程练习:
[root@lc test]# vi ex8.py
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
"ex8.py" 12L, 354C written
[root@lc test]#
执行该程序:
[root@lc test]# python ex8.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
[root@lc test]#
加分习题:注意最后一行程序中既有单引号又有双引号,你觉得它是如何工作的?
我觉得是默认打印单引号,但是因为第三句里面有个didn't单词自带有',所以第三句使用双引号。
以此类推,如果打印的字符串里面含有双引号,那么打印出来的语句也是单引号。
为了验证自己的想法,修改程序试试看结果:
[root@lc test]# vi ex8-2.py
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you'd type up right.", # modify to debug
"But it did not sing.", # modify to debug
'So I said "goodnight."' # modify to debug
)
"ex8-2.py" 12L, 408C written
[root@lc test]#
执行该程序:
[root@lc test]# python ex8-2.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' "That you'd type up right." 'But it did not sing.' 'So I said "goodnight."'
[root@lc test]#
看这次执行的结果和上次的比较,验证了自己的想法。
至此,笨方法学Python-ex8-打印打印这一小节的学习结束了。