python3打印格式输出
在python3中,如果我有一个列表[a、b、c],如何打印输出,例如:python3打印格式输出,python,string,python-3.x,format,Python,String,Python 3.x,Format,在python3中,如果我有一个列表[a、b、c],如何打印输出,例如: results: a b c 但我的输出是这样的: results: a b c 我的代码是 List = ['a', 'b', 'c'] print("results :", end = " ") for i in List: print(i) 如何格式化?在第一行之后的行中添加填充: lst = ['a', 'b', 'c'] pad = len('results:'
results: a
b
c
但我的输出是这样的:
results: a
b
c
我的代码是
List = ['a', 'b', 'c']
print("results :", end = " ")
for i in List:
print(i)
如何格式化?在第一行之后的行中添加填充:
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)
使用格式()
:
List = ['a', 'b', 'c']
print("results :", end = " ")
for i, e in enumerate(List):
if (i == 0):
print (e)
else:
print ("{:>{}}".format(e, len("results : ")))