textwrap 模块是一个使字符串按要求排列的模块。主要方式包括以下几种:

  • wrap - 返回格式化后的字符串列表,每一行为列表的一个元素。
  • fill - 按指定宽度填充字符串的每一行。
  • shorten - 略缩显示字符串。
  • dedent - 反缩进去除每行行首的空白。这方便显示三引号中的内容而不修改其源代码中的缩进。
  • indent - 添加缩进。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import textwrap

sample_text = '''
    The textwrap module can be used to format text for output in
    situations where pretty-printing is desired.\n     It offers
    programmatic functionality similar to the paragraph wrapping
    or filling features found in many text editors.
    '''

print("normal ->\n", sample_text)
print()
print("with textwrap.wrap ->\n", textwrap.wrap(sample_text, width = 50))
print()
print("with textwrap.wrap 2 ->\n", '\n'.join(textwrap.wrap(sample_text, width = 50)))
print()
print("with textwrap.fill width is 50 ->\n", textwrap.fill(sample_text, width = 50))
print()
print("with textwrap.fill width is 100 ->\n", textwrap.fill(sample_text, width = 100))
print()
print("with textwrap.shorten ->\n", textwrap.shorten(sample_text, width = 20))
print()
print("with textwrap.dedent ->\n", textwrap.dedent(sample_text))
print()
print("with textwrap.indent ->\n", textwrap.indent(sample_text, ">"))

output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
normal ->
 
    The textwrap module can be used to format text for output in
    situations where pretty-printing is desired.
     It offers
    programmatic functionality similar to the paragraph wrapping
    or filling features found in many text editors.
    

with textwrap.wrap ->
 ['     The textwrap module can be used to format', 'text for output in     situations where pretty-', 'printing is desired.      It offers', 'programmatic functionality similar to the', 'paragraph wrapping     or filling features found', 'in many text editors.']

with textwrap.wrap 2 ->
      The textwrap module can be used to format
text for output in     situations where pretty-
printing is desired.      It offers
programmatic functionality similar to the
paragraph wrapping     or filling features found
in many text editors.

with textwrap.fill width is 50 ->
      The textwrap module can be used to format
text for output in     situations where pretty-
printing is desired.      It offers
programmatic functionality similar to the
paragraph wrapping     or filling features found
in many text editors.

with textwrap.fill width is 100 ->
      The textwrap module can be used to format text for output in     situations where pretty-
printing is desired.      It offers     programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.

with textwrap.shorten ->
 The textwrap [...]

with textwrap.dedent ->
 
The textwrap module can be used to format text for output in
situations where pretty-printing is desired.
 It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.


with textwrap.indent ->
 
>    The textwrap module can be used to format text for output in
>    situations where pretty-printing is desired.
>     It offers
>    programmatic functionality similar to the paragraph wrapping
>    or filling features found in many text editors.