是否可以在一行上打印多个字符串,使得最终字符串始终距左侧 x 个空格?例如,是否有办法打印与此类似的结果,其中 strZ 始终打印在同一位置(不是右对齐)?
strA strB strC strZ
strA strZ
strC StrB strD strE strZ
最佳答案
使用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
关于python - Python 中的打印间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19661624/