首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python IDLE中换行文本

在Python IDLE中换行文本
EN

Stack Overflow用户
提问于 2018-05-17 12:35:38
回答 1查看 11.4K关注 0票数 3

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

EN

回答 1

Stack Overflow用户

发布于 2018-05-21 00:57:58

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

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

另一种方法是使用Python的string literal concatenation特性。空格由行结束引号保护,并且可以添加注释。(有关另一个示例,请参阅链接。)

代码语言:javascript
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50383404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档