Google colaboratory 你能在GoogleColab内的ipython笔记本上做一个文字包装吗?

Google colaboratory 你能在GoogleColab内的ipython笔记本上做一个文字包装吗?,google-colaboratory,Google Colaboratory,我想将python代码和输出用word包装在GoogleColab中的python笔记本中。有办法吗?在笔记本设置中设置垂直对齐对我来说并没有解决这个问题。您可以在Google Colab中使用python内置库“textwrap”执行类似操作 import textwrap wrapper = textwrap.TextWrapper(width=40, initial_indent=" " * 4, subsequent_indent=" " * 4, break_

我想将python代码和输出用word包装在GoogleColab中的python笔记本中。有办法吗?在笔记本设置中设置垂直对齐对我来说并没有解决这个问题。

您可以在Google Colab中使用python内置库“textwrap”执行类似操作

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.