Python: Evenly space output data with varying string lengths

前端 未结 4 502
走了就别回头了
走了就别回头了 2021-01-04 14:30

I am trying to get my output data to look like this:

-------------------------------------------------------
            Grade Report for Programs 
---------         


        
4条回答
  •  伪装坚强ぢ
    2021-01-04 15:05

    I find it simpler to use Python's builtin C-style formatting:

    >>> print("%-30s %4.1f" % ("Jacobson, Mark", 19.0))
    Jacobson, Mark                 19.0
    >>> 
    

    Note that a negative field width indicates that the field is to be left-justified.

提交回复
热议问题