Python right alignment

How can I justify the output of this code?

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

for i in range(N):
    case += '#'
    print(case)
+4
source share
4 answers

You can use formatwith >to justify correctly

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

Output

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

You can programmatically determine how far from the right alignment using rjust.

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

It looks like you can search for rjust:

https://docs.python.org/2/library/string.html#string.rjust

my_string = 'foo'
print my_string.rjust(10)
'       foo'
+6
source

string.format() .

print "{:>10}".format(case)

python, , case. , , case .

+3
N = int(input())
for i in range(N+1):
    print(" "*(N-i) + "#"*(i+1))

, "#".

+1

All Articles