Right Justify python

前端 未结 5 1867
失恋的感觉
失恋的感觉 2021-01-19 13:39

how can I justify the output of this code?

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

for i in range(N):
    case += \'#\'
    print(case)
5条回答
  •  生来不讨喜
    2021-01-19 13:46

    The string.format() method has this as part of its syntax.

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

    The number in the string tells python how many characters long the string should be, even if it's larger than the length of case. And the greater than sign tells it to right justify case within that space.

提交回复
热议问题