Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python3打印格式输出_Python_String_Python 3.x_Format - Fatal编程技术网

python3打印格式输出

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:'

在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:') * ' '  # 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 :  ")))