python string formatting Columns in line

前端 未结 4 2059
一生所求
一生所求 2020-11-27 20:26

I am trying to format the string so everything lines up between the two.

APPLES                           $.99                           214                          


        
4条回答
  •  孤街浪徒
    2020-11-27 20:54

    str.format() is making your fields left aligned within the available space. Use alignment specifiers to change the alignment:

    '<' Forces the field to be left-aligned within the available space (this is the default for most objects).

    '>' Forces the field to be right-aligned within the available space (this is the default for numbers).

    '=' Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types.

    '^' Forces the field to be centered within the available space.

    Here's an example (with both left and right alignments):

    >>> for args in (('apple', '$1.09', '80'), ('truffle', '$58.01', '2')):
    ...     print '{0:<10} {1:>8} {2:>8}'.format(*args)
    ...
    apple         $1.09       80
    truffle      $58.01        2
    

提交回复
热议问题