简体   繁体   中英

format Python output

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?

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   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM