Fixing right alignment of string formatting

前端 未结 1 963
北恋
北恋 2021-01-27 13:22

I\'m trying to align the output of the list like shown:

But it keeps coming out like this:

My code for all of this is:

subject_         


        
1条回答
  •  伪装坚强ぢ
    2021-01-27 14:05

    You can't do this using plain Python formatting because the amount of padding depends on both strings. Try defining a function such as:

    def format_justified(left, right, total):
        padding = total - len(left)
        return "{{0}}{{1:>{}}}".format(padding).format(left, right)
    

    then simply use:

    print(format_justified("Total cp:", total, 25))
    

    where 25 is the total line width you want.

    0 讨论(0)
提交回复
热议问题