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

2024-06-01 00:05:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

    Example Example Example Example Example Example Example Example Example Exam
    ple Example

在这方面:

^{pr2}$

有什么想法吗?在


Tags: 标题exampleexamplepr2
2条回答

你需要得到你要打印的东西的宽度。(可能是终点站)。然后在添加单词时跟踪字符串的长度。如果这个单词可以让python打印在两行上,那么在它之前使用一个换行符。此链接用于获取终端宽度How to get Linux console window width in Python

使用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.

相关问题 更多 >