Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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-textwrapper中的字符串格式_Python_String Formatting_Word Wrap - Fatal编程技术网

Python-textwrapper中的字符串格式

Python-textwrapper中的字符串格式,python,string-formatting,word-wrap,Python,String Formatting,Word Wrap,我有一份名单=[Tom,Mike,Carol],我想用下面的格式打印出来 People Tom Mike Carol 基本上,它的标题是“人”,一些固定的标签,然后是一个新的名称列表,看起来像一个表格。我想用Python中的textwrap实现这一点,但如果还有其他可能性,我也愿意这样做 dedented_text = textwrap.dedent(' This is a test sentecne to see how well the t

我有一份名单=[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

在上面,我添加了用于打印类别和句子的代码。但我无法实现我想要的

只需单独打印第一行,然后循环打印其他行:

print 'People ' + people[0]

for i in range(1, len(people)):
    print ' ' * 7 + people[i]

@巴加夫罗:对不起,忘了加上那个谢谢,但是如果我也想用同样的方式打印例句呢?我不希望句子的第二行从屏幕边缘开始。
print 'People ' + people[0]

for i in range(1, len(people)):
    print ' ' * 7 + people[i]