如何左对齐固定宽度的字符串? [英] How to left align a fixed width string?
本文介绍了如何左对齐固定宽度的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想要固定宽度的文本列,但字符串都是向右填充,而不是向左填充!!?
I just want fixed width columns of text but the strings are all padded right, instead of left!!?
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
推荐答案
你可以用 -
前缀来左对齐:
You can prefix the size requirement with -
to left-justify:
sys.stdout.write("%-6s %-50s %-25s\n" % (code, name, industry))
这篇关于如何左对齐固定宽度的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文