Print list items with a header
If I have a list defined as list=['Ford','Mu开发者_如何学编程stang','1966','red']
and try to print it my output would look like:
Ford Mustang 1966 red
But how can I achieve to have a heading so the output would look like:
Brand Modell Year Color
Ford Mustang 1966 Red
That would look much more professional.
LAYOUT = "{!s:10} {!s:14} {!s:4} {!s:10}"
yourList = ["Ford","Mustang",1966,"Red"]
print LAYOUT.format("Brand","Modell","Year","Color")
print LAYOUT.format(*yourList)
produces output in columns which will line up (so long as you don't exceed the field width specifiers!), ie
Brand Modell Year Color
Ford Mustang 1966 Red
Mercedes F-Cell 2011 White
You mean:
print 'Brand', 'Modell', 'Year', 'Color'
print ' '.join(yourList)
?
P.S.: Don't name your variable list
!
I'm not a python expert, but couldn't you just create an associative list, where the keys are the header names?
header = ['Brand', 'Modell', 'Year', 'Color']
car = ['Ford','Mustang','1966','red']
print("\t".join(header) + "\n" + "\t".join(car))
精彩评论