I have this python code which imports textwrap. However, i need to put a space infront of all lines which have a single digit at the front.
line1 = [" S M T W Th F S"]
line2 = ["--------------------"]
line3 = [" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"]
print(''.join(line1))
print(''.join(line2))
line3Str = ''.join(line3)
wrapper = textwrap.TextWrapper(width=20)
word_list = wrapper.wrap(text=line3Str)
for element in word_list:
print(element)
I have tried to put different spaces between the 7 and 8 or make a different list for the second line down but no matter what I encounter this issue.
Expected output: (Hyphen is space)
-1 ...
-8 ...
15 ...
22 ...
29 ...
Actual output:
-1 ...
8 ... <
15 ...
22 ...
29 ...
You want to right-align every line but last one, so you can replace last two lines in your program with:
for element in word_list[:-1]:
print(element.rjust(20))
print(word_list[-1])
Or, if you want to store the formatted list of strings, you can format word_list after calling wrapper.wrap:
word_list = wrapper.wrap(text=line3Str)
word_list = [x.rjust(20) for x in word_list[:-1]] + [word_list[-1]]