与字符串格式对齐 [英] Aligning with string formatting

查看:171
本文介绍了与字符串格式对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  GBP ...... 0.8639 ... 

我希望通过字符串格式来实现这种输出。 ... 0.8399

我已经尝试使用下面的代码(我代表currency和e [0]和e [1]是买卖价值):

pre code>{i:。< 10} {e [0] :。格式(i = i,e = e)

但是上面的代码并没有把第二个数字对齐到右边,但是它只是把它加到第一个数字后面,像这样:

<$ p $ $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $我解决了这个问题吗?

解决方案

作为一个通用的解决方案:

$ ul b $ b

  • 如果要将 右列,中间和左列分别对齐右侧,中间和左侧,请使用此格式字符串 p>

     >>> '{0:.10} {1:。^ 10} {2:。> 10}'格式('GBP',0.8639,0.8399)
    'GBP ........ .0.8639 ...... 0.8399'

    #又如
    >>>格式('GBPTEST',0.90,0.8)
    'GBPTEST ...... 0.9。'{0:.10} {1:。^ 10} {2:。> 10} .......... 0.8'


  • 您需要 除最右边的列之外的所有列都必须对齐

     >>> '{0:。< 10} {1:。> 10} {2:。> 10}'格式('GBP',0.8639,0.8399)
    'GBP ....... .... 0.8639 .... 0.8399'

    #另一个例子
    >>>格式('GBPTEST',0.90,0.8)
    'GBPTEST ....... ... 0.9 ....... 0.8'


  • 如果您希望 所有列都要对齐

     > ;>>格式('GBP',0.8639,0.8399)
    'GBP ....... 0.8639'{0:.10} {1:.10} {2: .... 0.8399'

    #另一个例子
    >>> ('GBPTEST',0.90,0.8)
    'GBPTEST ... 0.9 ....。{0:.10} {1:.10} {2: ... 0.8'




  • 更改对于每个参数,根据你需要的列的大小决定10个。




    下面是在问题中提及的那种params:

     >>>格式(i ='GBP',e =(0.8639,0.8399))$ b {e [0]:^ 10} {e [1]:。 $ b'GBP ......... 0.8639 ...... 0.8399'

    #又如
    >>>格式(i ='GBPTEST',e =(0.90,0.8))$ b(i = 10){e [0]:^ 10} {e [1]:。 $ b'GBPTEST ...... 0.9 ........... 0.8'


    I wish to achieve this sort of output with string formatting:

    GBP......0.8639......0.8399
    

    I have tried using the following code (i stands for currency and e[0] and e[1] are buy and sell values):

    "{i:.<10}{e[0]:.^6}{e[1]:.>6}".format(i=i, e=e)
    

    but the above code does not align the second number to the right, but it just adds it behind the first number, like this:

    GBP.......0.86390.8399
    

    How can I correct this?

    解决方案

    As a generalized solution:

    • Use this format string if you want the right column, center and left column to be aligned right, center and left respectively:

      >>> '{0:.<10}{1:.^10}{2:.>10}'.format('GBP', 0.8639, 0.8399)
      'GBP.........0.8639......0.8399'
      
      # Another example
      >>> '{0:.<10}{1:.^10}{2:.>10}'.format('GBPTEST', 0.90, 0.8)
      'GBPTEST......0.9...........0.8'
      

    • OR, this format string if you want all the columns except rightmost column to be aligned right:

      >>> '{0:.<10}{1:.>10}{2:.>10}'.format('GBP', 0.8639, 0.8399)
      'GBP...........0.8639....0.8399'
      
      # Another example
      >>> '{0:.<10}{1:.>10}{2:.>10}'.format('GBPTEST', 0.90, 0.8)
      'GBPTEST..........0.9.......0.8'
      

    • OR, this format string if you want all the columns to be aligned left:

      >>> '{0:.<10}{1:.<10}{2:<10}'.format('GBP', 0.8639, 0.8399)
      'GBP.......0.8639....0.8399    '
      
      # Another example
      >>> '{0:.<10}{1:.<10}{2:<10}'.format('GBPTEST', 0.90, 0.8)
      'GBPTEST...0.9.......0.8       '
      

    Change the value of 10 for each param based on the size of column you need.


    Below will be the format string specific for the kind of params as mentioned in the question:

    >>> '{i:.<10}{e[0]:.^10}{e[1]:.>10}'.format(i='GBP', e=(0.8639, 0.8399))
    'GBP.........0.8639......0.8399'
    
    # Another example
    >>> '{i:.<10}{e[0]:.^10}{e[1]:.>10}'.format(i='GBPTEST', e=(0.90, 0.8))
    'GBPTEST......0.9...........0.8'
    

    这篇关于与字符串格式对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆