源码

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."
)

输出结果
$ 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.'

问题1:formatter起到的作用
问题2:为什么最后一行输出的有单引号和双引号
第三句But it didn't sing为双引号,其余为单引号,观察发现变量其中已经包含了单引号,所以用双引号来括住,后面我在第一句中加了一个单引号,输出结果确实被双引号括住了,证实了我的猜想。
这是只是声明了一个变量formatter,使用字符串“%r %r %r %r"为其赋值
这里使用的%r,之后打印时可以使用% ()的方式进行赋值,如果不进行赋值,直接打印则输出”%r %r %r %r“字符串
分别打印了整数型,字符串,布尔值,以及变量(打印变量的时候会直接输出变量的值)。

注:1.%()里的值的个数必须和formatter中的%r的个数相同,按顺序对应
2.打印出的字符串默认用单引号括住,但如果字符串中本来就有单引号,那么会改用双引号括住。


ex9.py
源码

# Here's some new strange stuff,remember type it excatly.

days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print "Here are the days:",days
print "Here are the months:",months

print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want,or 5, or 6.
"""
输出结果

$ python ex9.py
Here are the days:  Mon Tue Wed Thu Fri Sat Sun
Here are the months:  Jan
Feb
Mar
Apr
May
Jun
Jul
Aug

There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
1.字符串中的"\n"可以换行

2.print关键字后面的""" """  ,两个三连双引号之间可以输入任意多行字符串内容,将其按输入的原格式打印出来。

 

ex10.py

源码