Skip to content

Commit d4efb36

Browse files
committed
p3
1 parent e7eb50b commit d4efb36

1 file changed

Lines changed: 23 additions & 23 deletions

File tree

‎109.md‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737
`.format()`是字符串的一个方法。你在交互模式中,输入`dir(str)`,会看到如下的内容:
3838

39-
    >>> dir(str)
40-
    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
39+
>>> dir(str)
40+
 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
4141

4242
这里所列出来的,就是字符串`str`的所有属性和方法。
4343

@@ -67,8 +67,8 @@
6767

6868
你暂且阅读文档,不理解也没关系。下面用示例来说明使用方法。
6969

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'
7272

7373
在交互模式中,输入了字符串`"I like {0} and {1}"`,并且其中用`{0}`和`{1}`占据了两个位置,它们就是占位符。然后使一个非常重要的点`.`。
7474

@@ -80,8 +80,8 @@
8080

8181
你还可以这样试试,就理解更深刻了。
8282

83-
    >>> "I like {1} and {0}".format("python", "canglaoshi")
84-
    'I like canglaoshi and python'
83+
>>> "I like {1} and {0}".format("python", "canglaoshi")
84+
 'I like canglaoshi and python'
8585

8686
请仔细观察找区别。
8787

@@ -93,18 +93,18 @@
9393

9494
既然是“格式化”,就要指定一些格式,让输出的结果符合指定的样式。
9595

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'
9898

9999
现在有格式了。`{0:10}`表示第一个位置,有10个字符那么长,并且放在这个位置的字符是左对齐;`{1:>15}`表示第二个位置,有15个字符那么长,并且放在这个位置的字符是右对齐。
100100

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 '
103103

104104
现在是居中对齐了。
105105

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 '
108108

109109
这个有点复杂,我们一点一点的解释。
110110

@@ -116,33 +116,33 @@
116116

117117
向`format()`中,除了能够传入字符串,还可以传入数字(包括整数和浮点数),而且也能有各种花样。
118118

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'
121121

122122
`{0:d}`表示在第一个位置放一个整数;`{1:f}`表示在第二个位置放一个浮点数,那么浮点数的小数位数,是默认的。下面在这个基础上,可以再做一些显示格式的优化。
123123

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'
126126

127127
`{0:d}`表示第一个位置的长度是4个字符,并且默认状态下,填充到该位置的整数是右对齐。
128128

129129
`{1:6.2f}`表示第二个位置的长度使6个字符,并且填充到该位置的浮点数要保留两位小数,默认也是右对齐。
130130

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'
133133

134134
`{0:04d}`和`{1:06.2f}`与前述例子不同的在于在声明位置长度的数字前面多了`0`,其含义是在数字前面,如果位数不足,则补`0`。
135135

136136
以上的输出方式中,我们只讨论了`format(*args, **kwargs)`中的`*args`部分。还有另外一种方式,则是与`**kwargs`有关的(关于这两种参数的含义,本教程后面有专门介绍)。
137137

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'
140140

141141
一种被称为“字典格式化”,这里仅仅列一个例子。关于“字典”,本教程后续会有的。
142142

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'
146146

147147
用`format()`做字符串格式化输出,真的很简洁,堪称优雅。
148148

0 commit comments

Comments
 (0)