Python 3 - convert the staircase from right-aligned to LEFT ALIGNED, composed of # symbols and spaces

Viewed 3027
sc = []
n = 6
for i in range(n):
    sc.append("#")
    scstr = ''.join(map(str, sc))
    print(scstr)

I tried to use the code below to reverse the output by adding padding white spaces but it prints out a distorted staircase.

# print(scstr.rjust(n-i, ' '))  -- trying to print reversed staircase

Please help convert the staircase from right-aligned to LEFT ALIGNED, composed of # symbols and spaces.

Attached is a visual description of expected output

Expected Output and my Output

4 Answers

You can use str.rjust()

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

I like the new string formatting.

Code:

for i in range(10, -1, -1):
    print("{0:#<10}".format(i*" "))

Produces:

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

You can "right align" a row by padding it with spaces. Here, the Ith rows should have N-I spaces and I hashes:

for i in range(1, n + 1):
    print(' ' * (n  - i) + '#' * i)

replace n with n+1

n = 6
for i in range(n+1):
    print(' '*(n-i) + '#' * i)
Related