【问题标题】:How do I print an integer with a set number of spaces before it?如何打印一个前面有一定数量空格的整数?
【发布时间】:2017-08-05 11:05:02
【问题描述】:

C 有printf("%Xd", Y);,它只打印整数 X 并使其在控制台窗口上占用 Y 个空格。

例如:

printf("%3d", 10);
console: " 10"`

printf("%5d", 5);
console: "    5"

如何在 python 3 中使用它?

【问题讨论】:

标签: python printing integer


【解决方案1】:

print("{0:10d}".format(5)) 将在 9 个空格后打印 5。 有关 python 格式的更多参考,请参阅this

【讨论】:

    【解决方案2】:

    一般情况下,我们可以使用字符串.format()

    >>> '{:<30}'.format('left aligned')
    'left aligned                  '
    >>> '{:>30}'.format('right aligned')
    '                 right aligned'
    >>> '{:^30}'.format('centered')
    '           centered           '
    >>> '{:*^30}'.format('centered')  # use '*' as a fill char
    '***********centered***********'
    

    我们也可以用 f 字符串 for python >3.6 来做到这一点

    >>> f"{'left aligned':<30}"
    'left aligned                  '
    >>> f"{'right aligned':>30}"
    '                 right aligned'
    >>> f"{'centered':^30}"
    '           centered           '
    >>> f"{'centered':*^30}"  # use '*' as a fill char
    '***********centered***********'
    

    【讨论】:

      【解决方案3】:

      我会这样做:

      >>> print(10*' ',10)
                 10
      >>> print(5*' ',5)
            5
      >>> x=5
      >>> print(x*' ',x)
            5
      >>> 
      

      【讨论】:

        【解决方案4】:

        你可以试试这个

        x = " "
        y = 10
        print((x*5), y)
        

        你的输出将是

             10
        

        您将 x 分配给带有空格的字符串,然后在打印时,只需将字符串与空格乘以 y 之前所需的空格数。

        【讨论】:

          【解决方案5】:

          使用ljustrjust

          这些 python 方法接受一个字符串和pad 它在左侧或右侧,使其成为chars 的固定编号,以便根据需要打印到控制台。

          .rjust为例:

          num = 1234
          chars = 9
          print(str(num).rjust(chars))
          

          这将输出:

          "     1234"
          

          或使用.ljust 会给你:

          "1234     "
          

          需要注意的一点是.ljust.rjust 与在int 之前或之后打印一定数量的空格' ' 明显不同,因为它们允许您指定总数 字符串的长度而不是它之前的空格。这是一个需要考虑的关键问题,例如在使用这些方法打印表格时,可以将 设置为 widths 使其整洁。而简单地预先添加 空格 会导致 宽度 的变化,因为 integersdigits 的数量会改变,从而导致 total 字符串长度改变使宽度变化。

          当然,如果您愿意,您仍然可以指定字符串长度,然后计算需要在int 之前或之后放置多少空格。

          所以.rjust 可能是这样的:

          num = 1234
          chars = 9
          print(' ' * (chars - len(str(num))) + str(num))
          

          注意到这里,我将num 作为字符串连接到带有+ 的空格上,而不是在打印语句中使用逗号,因为逗号会在两个。

          .ljust 相同,但num 先打印:

          print(str(num) + ' ' * (chars - len(str(num))))
          

          但是,考虑到您可以使用原始的两个内置 python 方法,所有这些工作都是不必要的,而且看起来很混乱 :)

          【讨论】:

            【解决方案6】:

            你可以在 python3 中使用类似 C 的 printf:

            你的 C printf:

            printf("%3d", 10);
            console: " 10"`
            
            printf("%5d", 5);
            console: "    5"
            

            在 Python3 中是:

            print("%3d" % (10));
            console: " 10"`
            
            print("%5d" %  (5));
            console: "    5"
            

            所以要在 Python3 中使用类似 C 的 printf,请使用“%”而不是“,”。
            之后,您只需要使用元组作为参数。

            更一般的情况(我从我的一个程序中获得调试跟踪):

            print("Router: %-5s, subnetToDest: %-20s with hop: %-20s" % (r, s[0], s[1]))
            Router: r0   , subnetToDest: 100.30.0.0/16        with hop: Local  
            

            PD,上例中的 - 表示右对齐。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-26
              • 2012-09-09
              • 1970-01-01
              • 1970-01-01
              • 2011-05-28
              • 1970-01-01
              相关资源
              最近更新 更多