Python-textwrapper中的字符串格式
我有一份名单=[Tom,Mike,Carol],我想用下面的格式打印出来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
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]