我正在写的字符串很长。为了便于阅读,我想将文本换成多行。这是怎么做到的。我以前读过说明,但现在找不到了。
发布于 2018-05-21 00:57:58
这个答案一般适用于Python,并不特定于IDLE。
对于没有嵌入换行符的长字符串的输入,可以输入多行字符串,然后删除换行符。但是,如果您希望行以逗号和句点后的空格结束,您将看不到它们,并且如果您从代码中去掉以行结尾的空格,它们将消失。(这对于CPython标准库代码是必需的。)
另一种方法是使用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.
https://stackoverflow.com/questions/50383404
复制相似问题