格式化Python输出(format Python output)

编程入门 行业动态 更新时间:2024-10-21 03:50:50
格式化Python输出(format Python output)

我正在使用template.format()来对齐两列,就像这样

a 1 b 3 c 6

我尝试了以下方法:

template = "{0:30}{2:7}" w.write("| "+(template.format("a",len(a.findall(info)))+"\n")) w.write("| "+template.format("b",count)+"\n")

我有这个

a 1 b 5

它没有对齐为什么会发生这种情况? 任何人都可以帮助我吗?

I'm using template.format() to have two columns aligned, like this

a 1 b 3 c 6

I tried the following:

template = "{0:30}{2:7}" w.write("| "+(template.format("a",len(a.findall(info)))+"\n")) w.write("| "+template.format("b",count)+"\n")

I had this

a 1 b 5

it is not aligned why did this happen? any one can help me?

最满意答案

我认为你的模板是错误的。 请看看这个:

template = "{:30}{:7}" print(template.format("agasdf","1")) print(template.format("b","ddsfg5ggg")) print(template.format("hgfdhb","5ggg"))

在列中提供了很好的格式:

agasdf 1 b ddsfg5ggg hgfdhb 5ggg

I think your template is wrong. Please have a look at this:

template = "{:30}{:7}" print(template.format("agasdf","1")) print(template.format("b","ddsfg5ggg")) print(template.format("hgfdhb","5ggg"))

Gives nice formatting in columns:

agasdf 1 b ddsfg5ggg hgfdhb 5ggg

更多推荐

本文发布于:2023-07-06 08:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047609.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   format   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!