Python textwrapp中的字符串格式

2024-05-13 18:10:16 发布

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

我有一个People=[Tom,Mike,Carol]的列表,我想按下面的格式打印出来。你知道吗

People    Tom
          Mike
          Carol

基本上它是标题(人物)一些固定的标签,然后是一个名字列表在新行中(它看起来有点像一个表)。我想在Python中使用textwrap来实现这一点,但是如果还有其他可能的话,我也愿意这样做。你知道吗

dedented_text = textwrap.dedent(' This is a test sentecne to see how well   the textwrap works and hence using it here to test it').strip()
for width in [ 20 ]:
    h='{0: <4}'.format('f')
    k='{0: >4}'.format(textwrap.fill(dedented_text, width=20))
    print h+k

输出:

f   This is a test
sentecne to see how
well the textwrap
works and hence
using it here to
test it

上面我添加了打印类别和句子的代码。但我不能实现我想要的


Tags: totexttest列表isitthispeople