gpt4 book ai didi

python-idle - 在 Python IDLE 中包装文本

转载 作者:行者123 更新时间:2023-12-05 07:33:52 26 4
gpt4 key购买 nike

我写的字符串很长。为了便于阅读,我想将文本换行到多行。这是怎么做到的。我以前看过说明,但现在找不到了。

最佳答案

[这个答案一般适用于 Python,并不特定于 IDLE。]

对于没有嵌入换行符的长字符串输入,您可以输入多行字符串,然后删除换行符。但是,如果您希望行在逗号和句号之后以空格结尾,您将看不到它们,并且如果您从代码中去除行结尾的空白,它们就会消失。 (这是 CPython stdlib 代码所必需的。)

另一种方法是使用 Python 的 string literal concatenation特征。空白由行尾引号保护,可以添加注释。 (请参阅另一个示例的链接。)

stories = {
'John' : "One day John went to the store to buy a game. " # the lead
"The name of the game was Super Blaster. " # the hint
"On the way to the store, John was blasted by a purple ray. "
"The ray of purple light, mixed with super neutrinos, "
"came from a alien spaceship hovering above."
}

import textwrap
print('\n'.join(textwrap.wrap(stories['John'])))

# prints

One day John went to the store to buy a game. The name of the game
was Super Blaster. On the way to the store, John was blasted by a
purple ray. The ray of purple light, mixed with super neutrinos, came
from a alien spaceship hovering above.

关于python-idle - 在 Python IDLE 中包装文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50383404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com