第一章:文本-textwrap:格式化文本段落-结合dedent和fill

1.2.4 结合dedent和fill
接下来,可以把去除缩进的文本传入fill(),并指定一些不同的width值。

import textwrap
from textwrap_example import sample_text

dedented_text = textwrap.dedent(sample_text).strip()
for width in [45,60]:
    print('{} Columns:\n'.format(width))
    print(textwrap.fill(dedented_text,width=width))
    print()

运行结果:

45 Columns:

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 un many
text editors.

60 Columns:

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 un many text editors.

猜你喜欢

转载自blog.csdn.net/weixin_43193719/article/details/86654484