gpt4 book ai didi

python - 如何左对齐固定宽度的字符串?

转载 作者:IT老高 更新时间:2023-10-28 20:23:27 26 4
gpt4 key购买 nike

我只想要固定宽度的文本列,但字符串都向右填充,而不是向左填充!!?

 sys.stdout.write("%6s %50s %25s\n" % (code, name, industry))

生产

BGA                                BEGA CHEESE LIMITED   Food Beverage & Tobacco
BHP BHP BILLITON LIMITED Materials
BGL BIGAIR GROUP LIMITED Telecommunication Services
BGG BLACKGOLD INTERNATIONAL HOLDINGS LIMITED Energy

但我们想要

BGA BEGA CHEESE LIMITED                                Food Beverage & Tobacco
BHP BHP BILLITON LIMITED Materials
BGL BIGAIR GROUP LIMITED Telecommunication Services
BGG BLACKGOLD INTERNATIONAL HOLDINGS LIMITED Energy

最佳答案

您可以在大小要求前加上 - 以左对齐:

sys.stdout.write("%-6s %-50s %-25s\n" % (code, name, industry))

关于python - 如何左对齐固定宽度的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684368/

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