多个字符串对齐

时间:2018-10-31 14:04:51

标签: python string python-3.x templates alignment

所以这是我的问题,我有一个程序应该根据用户输入的列数和空格来打印多行数字:

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()

但是如果我输入例如:

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

我明白了:

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 

而不是: expected output

我尝试使用{:<},{:-}等格式方法,但它最多只能用于两个或三个不同的输入,而不是全部输入。

预先感谢

1 个答案:

答案 0 :(得分:0)

要获得所需的输出,请替换

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

使用

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

这是最小的改进。 2是最大位数的位数。您应该根据e_num值-len(str(e_num))进行计算,并使用该值来创建要格式化和打印的适当字符串:

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

然后在for循环中使用它:

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