我写的字符串很长。为了便于阅读,我想将文本包装成多行。怎么做我之前已阅读过说明,但现在无法找到它们。
[这个答案一般适用于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.