2

有没有人有一种简洁的方式来完全证明给定线宽的字符串,如下所示:

The cat sat on the mat, the cat
sat on the mat, the  cat sat on
mat, the cat sat on the mat the
mat,  the cat  sat on  the  mat
the cate sat on the mat...

这样两边的边缘都是直的。该函数接受一个字符串和一个线宽,并以上述格式返回一个字符串。有左、右和中心的 Python 函数,但不合理。我有一个方法,但它有点太长了。

谢谢!

捷通

4

1 回答 1

3

这是 ActiveState Python Cookbook 中的一个秘诀:

“将字符串与单词之间的空格对齐以适应指定的宽度”

于 2013-04-03T15:01:24.357 回答