正确对齐python

Kno*_*oot 4 python python-3.x

我怎样才能证明这段代码的输出是正确的?

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

for i in range(N):
    case += '#'
    print(case)
Run Code Online (Sandbox Code Playgroud)

Cor*_*mer 13

你可以用format与>右对齐

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

产量

         #
        ##
       ###
      ####
     #####
    ######
   #######
  ########
 #########
##########
Run Code Online (Sandbox Code Playgroud)

您可以通过编程方式计算出使用权利的合理范围rjust.

for i in range(1, N+1):
    print(('#'*i).rjust(N))
Run Code Online (Sandbox Code Playgroud)


mat*_*bet 7

好像你可能正在寻找rjust:

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

my_string = 'foo'
print my_string.rjust(10)
'       foo'
Run Code Online (Sandbox Code Playgroud)