Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str.format() -> how to left-justify

Tags:

python

>>> print 'there are {0:10} students and {1:10} teachers'.format(scnt, tcnt)
there are        100 students and         20 teachers

What would be the code so that the output became:

there are 100        students and 20         teachers

Thanks.

like image 892
jd. Avatar asked May 06 '09 13:05

jd.


1 Answers

print 'there are {0:<10} students and {1:<10} teachers'.format(scnt, tcnt)

While the old % operator uses - for alignment, the new format method uses < and >

like image 75
Eli Courtwright Avatar answered Nov 15 '22 16:11

Eli Courtwright