我正在写的字符串很长。为了更容易阅读,我想将文本换成多行。这是怎么做的。我之前已阅读说明,但现在无法找到它们。
12245 次
1 回答
0
[这个答案一般适用于 Python,并不特定于 IDLE。]
对于没有嵌入换行符的长字符串的输入,您可以输入多行字符串,然后删除换行符。但是,如果您希望行以逗号和句点后的空格结尾,您将看不到它们,如果您从代码中去除行尾空格,它们就会消失。(这是 CPython 标准库代码所必需的。)
另一种方法是使用 Python 的字符串文字连接功能。空格由行尾引号保护,并且可以添加注释。(有关另一个示例,请参见链接。)
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.
于 2018-05-20T16:57:58.357 回答