Right Justify python

前端 未结 5 1869
失恋的感觉
失恋的感觉 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:41

    One liner using f-string and join:

    print("\n".join([f"{'#' * i:>10}" for i in range(1, 11)]))
    

    Output:

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

    If you would like to include row number you can do following:

    print("\n".join([f"{i:<3}{'#' * i:>10}" for i in range(1, 11)]))
    

    Output:

    1           #
    2          ##
    3         ###
    4        ####
    5       #####
    6      ######
    7     #######
    8    ########
    9   #########
    10 ##########
    

提交回复
热议问题