use*_*070 2 python string-formatting
是否可以在一行上打印多个字符串,使得最终字符串始终距左侧 x 个空格?例如,是否有办法打印与此类似的结果,其中 strZ 始终打印在同一位置(不是右对齐)?
strA strB strC strZ
strA strZ
strC StrB strD strE strZ
Run Code Online (Sandbox Code Playgroud)
使用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'))
Run Code Online (Sandbox Code Playgroud)
印刷
strA strB strC strZ
strA strZ
strA strB strC strE strZ
Run Code Online (Sandbox Code Playgroud)
请参阅格式化字符串语法。
| 归档时间: |
|
| 查看次数: |
7076 次 |
| 最近记录: |