Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right Justify python

how can I justify the output of this code?

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

for i in range(N):
    case += '#'
    print(case)
like image 874
Knox Root Avatar asked Nov 27 '22 20:11

Knox Root


1 Answers

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))
like image 78
Cory Kramer Avatar answered Dec 06 '22 16:12

Cory Kramer