2014-03-26 52 views

回答

0

編輯:有人更改了問題的一個關鍵細節 阿疣winiचhaudhary給出了一個很好的答案。 /如果你是不是在學習的位置使用的String.Format現在則解決問題的更普遍的/算法的方法是這樣的:

for (name, score) in students: 
    print '%s%s%s\n'%(name,' '*(10-len(name)),score) 
5

使用string formatting

>>> students = [("Abe", 200), ("Lindsay", 180), ("Rachel" , 215)] 
>>> for a, b in students: 
...  print '{:<7s} {}'.format(a, b) 
... 
Abe  200 
Lindsay 180 
Rachel 215 
0

使用rjust和ljust:

for s in students: 
    print s[0].ljust(8)+(str(s[1])).ljust(3) 

輸出:

Abe  200 
Lindsay 180 
Rachel 215