Skip to content

Update p13_aligning_text_strings.rst #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions source/c02/p13_aligning_text_strings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

>>> text = 'Hello World'
>>> text.ljust(20)
'Hello World '
'Hello World '
>>> text.rjust(20)
' Hello World'
' Hello World'
>>> text.center(20)
' Hello World '
' Hello World '
>>>
所有这些方法都能接受一个可选的填充字符。比如:

Expand All @@ -40,11 +40,11 @@
.. code-block:: python

>>> format(text, '>20')
' Hello World'
' Hello World'
>>> format(text, '<20')
'Hello World '
'Hello World '
>>> format(text, '^20')
' Hello World '
' Hello World '
>>>

如果你想指定一个非空格的填充字符,将它写到对齐字符的前面即可:
Expand All @@ -62,7 +62,7 @@
.. code-block:: python

>>> '{:>10s} {:>10s}'.format('Hello', 'World')
' Hello World'
' Hello World'
>>>

``format()`` 函数的一个好处是它不仅适用于字符串。它可以用来格式化任何值,使得它非常的通用。
Expand All @@ -72,9 +72,9 @@

>>> x = 1.2345
>>> format(x, '>10')
' 1.2345'
' 1.2345'
>>> format(x, '^10.2f')
' 1.23 '
' 1.23 '
>>>

|
Expand All @@ -87,9 +87,9 @@
.. code-block:: python

>>> '%-20s' % text
'Hello World '
'Hello World '
>>> '%20s' % text
' Hello World'
' Hello World'
>>>

但是,在新版本代码中,你应该优先选择 ``format()`` 函数或者方法。
Expand Down