1

In python3 , if I have a list[a,b,c], how can I print the output such as :

results: a 
         b
         c  

but my output is like:

results: a
b
c

My code is

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

How can I format it?

2 Answers 2

1

Add padding for the lines after the first line:

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)
Sign up to request clarification or add additional context in comments.

Comments

0

using format():

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.