您可以在Google Colab的ipython笔记本上进行自动换行吗?

时间:2020-01-28 19:57:46

标签: google-colaboratory

我想在Google colab内的python笔记本中自动包装python代码和输出。有没有办法做到这一点 ?在笔记本设置中设置垂直对齐方式对我来说并不能解决此问题。

1 个答案:

答案 0 :(得分:0)

您可以使用python内置的lib'textwrap'在Google Colab中执行类似的操作

import textwrap

wrapper = textwrap.TextWrapper(width=40,
    initial_indent=" " * 4,
    subsequent_indent=" " * 4,
    break_long_words=False,
    break_on_hyphens=False)

string = "PURPOSE: To end world hunger, create peace for all people, solve all technological and scientific problems, make an exorbitant amount of money, and remain humble in the process."
print wrapper.fill(string)

输出

    PURPOSE: To end world hunger, create peace for all people, solve all
    technological and scientific problems, make an exorbitant amount of money,
    and remain humble in the process.