Right Justify python

前端 未结 5 1865
失恋的感觉
失恋的感觉 2021-01-19 13:39

how can I justify the output of this code?

N = int(input())
case = \'#\'
print(case)

for i in range(N):
    case += \'#\'
    print(case)
5条回答
  •  生来不讨喜
    2021-01-19 13:55

    You can use format with > to right justify

    N = 10
    for i in range(1, N+1):
        print('{:>10}'.format('#'*i))
    

    Output

             #
            ##
           ###
          ####
         #####
        ######
       #######
      ########
     #########
    ##########
    

    You can programattically figure out how far to right-justify using rjust as well.

    for i in range(1, N+1):
        print(('#'*i).rjust(N))
    

提交回复
热议问题