I am trying to solve an exercise. I should write a function that takes a list of integers, converts it to a string of numbers displayed vertically.
mylist = [5,69,2090]
the function should return/print the following string:
2
0
6 9
5 9 0
I tried to solve it with the following code, but it doesn't help me
def printstring(mylist):
h = len(mylist)//3
for i in range(h):
print(mylist[i],mylist[h+i],l[h+i+1])
First convert your list items to a string then find the maximum length. Pad your strings to fit that length and print them:
lst = [5, 69, 2090]
lst = list(map(str, lst))
max_lenght = max(map(len, lst))
padded_lst = [item.rjust(max_lenght, " ") for item in lst]
for row in zip(*padded_lst):
print(" ".join(row))
2
0
6 9
5 9 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With