Python间距和对齐字符串 [英] Python spacing and aligning strings

查看:184
本文介绍了Python间距和对齐字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不使用的情况下增加间距以在两个字符串变量之间对齐文本。



试图使文本看起来像这样,

 位置:10-10-10-10修订版:1 
地区:塔楼日期:2012年5月16日
用户:LOD时间:10:15

当前已对其进行了编码像这样,只用空格...

 位置: +位置+版本: +版本+'\ n'

我尝试使用string.rjust&

建议?

解决方案

您应该可以使用format方法:

 位置:{0:20}修订版{1}。format(位置,修订版)

您将必须根据每行的格式长度来确定标签的长度。用户行所需的格式宽度应比位置行或地区行宽。


I am trying to add spacing to align text in between two strings vars without using " " to do so

Trying to get the text to look like this, with the second column being aligned.

Location: 10-10-10-10       Revision: 1
District: Tower             Date: May 16, 2012
User: LOD                   Time: 10:15

Currently have it coded like this, just using spaces...

"Location: " + Location + "               Revision: " + Revision + '\n'

I tried working with string.rjust & srting.ljust but to no avail.

Suggestions?

解决方案

You should be able to use the format method:

"Location: {0:20} Revision {1}".format(Location,Revision)

You will have to figure out the of the format length for each line depending on the length of the label. The User line will need a wider format width than the Location or District lines.

这篇关于Python间距和对齐字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆