|
67 | 67 |
|
68 | 68 | 你暂且阅读文档,不理解也没关系。下面用示例来说明使用方法。 |
69 | 69 |
|
70 | | - >>> "I like {0} and {1}".format("python", "canglaoshi") |
71 | | - 'I like python and canglaoshi' |
| 70 | + >>> "I like {0} and {1}".format("python", "canglaoshi") |
| 71 | + 'I like python and canglaoshi' |
72 | 72 |
|
73 | 73 | 在交互模式中,输入了字符串`"I like {0} and {1}"`,并且其中用`{0}`和`{1}`占据了两个位置,它们就是占位符。然后使一个非常重要的点`.`。 |
74 | 74 |
|
|
81 | 81 | 你还可以这样试试,就理解更深刻了。 |
82 | 82 |
|
83 | 83 | >>> "I like {1} and {0}".format("python", "canglaoshi") |
84 | | - 'I like canglaoshi and python' |
| 84 | + 'I like canglaoshi and python' |
85 | 85 |
|
86 | 86 | 请仔细观察找区别。 |
87 | 87 |
|
|
93 | 93 |
|
94 | 94 | 既然是“格式化”,就要指定一些格式,让输出的结果符合指定的样式。 |
95 | 95 |
|
96 | | - >>> "I like {0:10} and {1:>15}".format("python", "canglaoshi") |
97 | | - 'I like python and canglaoshi' |
| 96 | + >>> "I like {0:10} and {1:>15}".format("python", "canglaoshi") |
| 97 | + 'I like python and canglaoshi' |
98 | 98 |
|
99 | 99 | 现在有格式了。`{0:10}`表示第一个位置,有10个字符那么长,并且放在这个位置的字符是左对齐;`{1:>15}`表示第二个位置,有15个字符那么长,并且放在这个位置的字符是右对齐。 |
100 | 100 |
|
101 | | - >>> "I like {0:^10} and {1:^15}".format("python", "canglaoshi") |
102 | | - 'I like python and canglaoshi ' |
| 101 | + >>> "I like {0:^10} and {1:^15}".format("python", "canglaoshi") |
| 102 | + 'I like python and canglaoshi ' |
103 | 103 |
|
104 | 104 | 现在是居中对齐了。 |
105 | 105 |
|
106 | | - >>> "I like {0:.2} and {1:^10.4}".format("python", "canglaoshi") |
107 | | - 'I like py and cang ' |
| 106 | + >>> "I like {0:.2} and {1:^10.4}".format("python", "canglaoshi") |
| 107 | + 'I like py and cang ' |
108 | 108 |
|
109 | 109 | 这个有点复杂,我们一点一点的解释。 |
110 | 110 |
|
|
116 | 116 |
|
117 | 117 | 向`format()`中,除了能够传入字符串,还可以传入数字(包括整数和浮点数),而且也能有各种花样。 |
118 | 118 |
|
119 | | - >>> "She is {0:d} years old and the breast is {1:f}cm".format(28, 90.1415926) |
120 | | - 'She is 28 years old and the breast is 90.141593cm' |
| 119 | + >>> "She is {0:d} years old and the breast is {1:f}cm".format(28, 90.1415926) |
| 120 | + 'She is 28 years old and the breast is 90.141593cm' |
121 | 121 |
|
122 | 122 | `{0:d}`表示在第一个位置放一个整数;`{1:f}`表示在第二个位置放一个浮点数,那么浮点数的小数位数,是默认的。下面在这个基础上,可以再做一些显示格式的优化。 |
123 | 123 |
|
124 | | - >>> "She is {0:4d} years old and the breast is {1:6.2f}cm".format(28, 90.1415926) |
125 | | - 'She is 28 years old and the breast is 90.14cm' |
| 124 | + >>> "She is {0:4d} years old and the breast is {1:6.2f}cm".format(28, 90.1415926) |
| 125 | + 'She is 28 years old and the breast is 90.14cm' |
126 | 126 |
|
127 | 127 | `{0:d}`表示第一个位置的长度是4个字符,并且默认状态下,填充到该位置的整数是右对齐。 |
128 | 128 |
|
129 | 129 | `{1:6.2f}`表示第二个位置的长度使6个字符,并且填充到该位置的浮点数要保留两位小数,默认也是右对齐。 |
130 | 130 |
|
131 | | - >>> "She is {0:04d} years old and the breast is {1:06.2f}cm".format(28, 90.1415926) |
132 | | - 'She is 0028 years old and the breast is 090.14cm' |
| 131 | + >>> "She is {0:04d} years old and the breast is {1:06.2f}cm".format(28, 90.1415926) |
| 132 | + 'She is 0028 years old and the breast is 090.14cm' |
133 | 133 |
|
134 | 134 | `{0:04d}`和`{1:06.2f}`与前述例子不同的在于在声明位置长度的数字前面多了`0`,其含义是在数字前面,如果位数不足,则补`0`。 |
135 | 135 |
|
136 | 136 | 以上的输出方式中,我们只讨论了`format(*args, **kwargs)`中的`*args`部分。还有另外一种方式,则是与`**kwargs`有关的(关于这两种参数的含义,本教程后面有专门介绍)。 |
137 | 137 |
|
138 | | - >>> "I like {lang} and {name}".format(lang="python", name="canglaoshi") |
139 | | - 'I like python and canglaoshi' |
| 138 | + >>> "I like {lang} and {name}".format(lang="python", name="canglaoshi") |
| 139 | + 'I like python and canglaoshi' |
140 | 140 |
|
141 | 141 | 一种被称为“字典格式化”,这里仅仅列一个例子。关于“字典”,本教程后续会有的。 |
142 | 142 |
|
143 | | - >>> data = {"name":"Canglaoshi", "age":28} |
144 | | - >>> "{name} is {age}".format(**data) |
145 | | - 'Canglaoshi is 28' |
| 143 | + >>> data = {"name":"Canglaoshi", "age":28} |
| 144 | + >>> "{name} is {age}".format(**data) |
| 145 | + 'Canglaoshi is 28' |
146 | 146 |
|
147 | 147 | 用`format()`做字符串格式化输出,真的很简洁,堪称优雅。 |
148 | 148 |
|
|
0 commit comments