Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何防止python在两行中打断一个单词?_Python_Python 2.7 - Fatal编程技术网

如何防止python在两行中打断一个单词?

如何防止python在两行中打断一个单词?,python,python-2.7,Python,Python 2.7,我所说的标题是指我如何改变这一点: Example Example Example Example Example Example Example Example Example Exam ple Example 为此: Example Example Example Example Example Example Example Example Example Example Example 有什么想法吗?使用textwrap模块 从文档中: textwr

我所说的标题是指我如何改变这一点:

    Example Example Example Example Example Example Example Example Example Exam
    ple Example
为此:

    Example Example Example Example Example Example Example Example Example 
    Example Example

有什么想法吗?

使用
textwrap
模块

从文档中:

textwrap.wrap(text[, width[, ...]])

    Wraps the single paragraph in text (a string) so every
    line is at most width characters long. Returns a list
    of output lines, without final newlines.

您需要获得打印对象的宽度。(可能是终点站)。然后在添加单词时跟踪字符串的长度。如果这个单词让python在两行上打印,那么在它前面使用换行符。此链接用于获取终端宽度

这是字符串吗?还是python代码?可能文字换行只是您正在使用的文本编辑器的一个功能。在解释器中?在GUI中?在终端窗口?在你的编辑器中?我指的是印刷版的“示例…”,我想用python来包装它,我在IDLE中也提到了这一点。如果我不够具体,我很抱歉,我对这一点很陌生。所以在这种情况下,我会做一些类似的事情:textwrap.wrap(“示例”[,60[,30]])是的,但没有方括号,这只是文档的一部分。您还可以传递其他各种参数。换行到一个20个字符的行:textwrap.wrap(“示例”,20)。