gpt4 book ai didi

python - Python 中的打印间距

转载 作者:行者123 更新时间:2023-12-03 09:26:40 25 4
gpt4 key购买 nike

是否可以在一行上打印多个字符串,使得最终字符串始终距左侧 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

参见Format String Syntax .

关于python - Python 中的打印间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19661624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com