我正在使用 Python 从 Web 服务收集数据。数据本身是一个将呈现给用户的列表。我已经设法将它打印出来,如下所示:
( 1) Example of strings
( 2) Example 4
( 3) Another Example
( 4) Another Example 2
我使用 rjust (2) 作为数字。但是,如果线条很长,则打印会像。断开宽度是终端的长度(rxvt,gnome-terminal):
( 1) Example of a very very very very long string and
that doesn't look fine
( 2) Example of indented long line that is very very
long, example line
( 3) Another Example
( 4) Another Example of a very very long string and
whatever here is
但我想要的是一个打印出来,如:
( 1) Example of a very very very very long string and
that doesn't look fine
( 2) Example of indented long line that is very very
long, example line
( 3) Another Example
( 4) Another Example of a very very long string and
whatever here is
是否有任何想法如何在不手动拆分线条的情况下打印上述线条?
最佳答案
使用 textwrap 模块:http://docs.python.org/library/textwrap.html
textwrap.fill(text, initial_indent='', subsequent_indent=' ')
关于python - 使用print()时如何换行和缩进长行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4355061/