Python 3 - convert right-aligned ladder to LEFT ALIGNED consisting of # characters and spaces

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

I tried using the code below to cancel the output by adding white spaces, but it prints a distorted ladder.

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

Please help convert the right-aligned staircase to LEFT ALIGNED consisting of # characters and spaces.

A visual description of the expected exit is included.

Expected Conclusion and My Conclusion

+4
source share
4 answers

You can "align the right" line by filling it with spaces. Here, the ith line should have NI spaces and hashes:

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

I like the formatting of newlines.

The code:

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

It produces:

         #
        ##
       ###
      ####
     #####
    ######
   #######
  ########
 #########
##########
+4
source

str.rjust()

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

n n + 1

sc = []
n = 6
for i in range(n+1):
    print(' '*(n-i) + '#' * i)
+1
source

Source: https://habr.com/ru/post/1689814/


All Articles