Python中的打印间距

时间:2013-10-29 14:52:54

标签: python string-formatting

是否可以在一行上打印多个字符串,使得最终字符串始终是左边的x空格量?例如,是否有打印类似于此的结果,其中strZ始终打印在相同的位置(不是右对齐)?

strA strB strC        strZ 
strA                  strZ 
strC StrB strD strE   strZ

1 个答案:

答案 0 :(得分:2)

使用str.format

fmt = '{:<4} {:<4} {:<4} {:<6} {:<4}'
print(fmt.format('strA', 'strB', 'strC', '', 'strZ'))
print(fmt.format('strA', '', '', '', 'strZ'))
print(fmt.format('strA', 'strB', 'strC', 'strE', 'strZ'))

打印

strA strB strC        strZ
strA                  strZ
strA strB strC strE   strZ

请参阅Format String Syntax

相关问题