Search code examples
pythonstringpython-3.xtemplatesalignment

Multiple strings to align


So here is my problem, I have this program that is supposed to print multiple lines of numbers according to the number of columns and spaces the user inputs:

def main():

from string import Template             # We import the Template method as seen in python glossary 7.1.4

s_num, e_num = [int(x) for x in input("Please enter a starting number and an ending number separated by a comma: ").split(',')] #We define the variables, s_num and e_num, as the respective starting point and ending point of the range that the user will input. In addition we call the int() function to make sure the input is an integer.

column, space = [int(x) for x in input("Please enter the number of numbers printed on a row and the number of spaces per item separated by a comma: ").split(',')] #We define the variables, column and space, as the respective amount of rows and amount of spaces that the user will input. In addition we call the int() function to make sure the input is an integer.


print('------------------------------') # We print a line of indents as in the sample
print('\n')
print ('Your print-out of numbers {}-{} using {} colums and {} spaces between numbers:'.format(s_num, e_num, column, space)) #We use the format method to implement the four variables created in the line and including them in the string phrase.

a = Template('$spce')                   # We create a variable "a" with one template "spce" that we will modify afterwards to implement in it the number of spaces that the user has inputted.
b = a.substitute(spce = ' ' * (space))  # We create a second variable b that will substitute the template "spce"  with the number of spaces, by multipling the integers in the space variable by, ' ', a normal space.

counter = 1                             # We create a variable called counter that will count the number of lines, in order to know when to go back to line.
for i in range(s_num,e_num + 1):        # We invoke a "for" loop in the range of numbers inputted by the user. In addition we add "+1", to also take the last integer of the range. 
    print ('{}{}'.format(i,b),end='')   # We implement the ".format" method  as an alternative to print(i+b) since i and b are from different types, and finally we add " end='' " to get back to the line.
    if (counter % column == 0 ):        # We invoke a conditional statement, so that the program knows when to get back to the line:
            print()                     # In that respect, if the number of lines divided by the amount of rows is nill: then the program goes back to the line.
    counter = counter +  1              # We add an additional line to the counter variable, that is counting the lines, and we iterate all over again until all the numbers are printed


main()

But if i input for instance :

Please enter a starting number and an ending number separated by 
a comma: 1,32
Please enter the number of numbers printed on a row and the 
number of spaces per item separated by a comma: 10,4

I get :

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    32 

Rather than : expected output

I tried the format methods using : {:<},{:-},etc but it works at most for two or three different inputs, rather than all of them.

Thanks in advance


Solution

  • To get the desired output replace

    print ('{}{}'.format(i,b),end='')
    

    with

    print ('{: >2}{}'.format(i,b),end='')
    

    This is minimal improvement. Here 2 is the amount of digits in the largest number. You should calculate it from e_num value - len(str(e_num)) and use the value to create appropriate string to be formatted and printed:

    templ = '{' + ': >{}'.format(len(str(e_num))) + '}{}'
    

    And then use it inside your for loop:

    print (templ.format(i,b),end='')