2016-03-25 19 views
1

、私はリスト[a、b、c]のを持っている場合、どのように私はのような出力印刷することができます。のpython3の印刷形式の出力

results: a 
     b 
     c 

を私の出力は以下のようである:

results: a 
b 
c 

私のコードは

List = ['a', 'b', 'c'] 
print("results :", end = " ") 
for i in List: 
    print(i) 

どのように私はそれをフォーマットすることができますか?

答えて

1

最初の行の後の行のためのパディングを追加します。format()を使用して

lst = ['a', 'b', 'c'] 
pad = len('results:') * ' ' # Number of spaces to insert (2nd, 3rd, ... lines) 
for i, x in enumerate(lst): 
    if i == 0: 
     print('results:', x) 
    else: 
     print(pad, x) 
0

List = ['a', 'b', 'c'] 
print("results :", end = " ") 
for i, e in enumerate(List): 
    if (i == 0): 
     print (e) 
    else: 
     print ("{:>{}}".format(e, len("results : ")))