【问题标题】:How to do print formatting in Python with chunks of strings?如何在 Python 中使用字符串块进行打印格式化?
【发布时间】:2016-07-23 01:46:10
【问题描述】:

我在格式化金字塔时遇到了一些问题。从循环打印时,我尝试使用格式,但这似乎不起作用,只是破坏了程序。格式化输出的不同方法是什么。我遇到的唯一麻烦是当我打印 10 及以上时,当有两位数时。格式化打印输出的最佳方法是什么?我尝试了多种方法,但无法在文档的循环中进行格式化 https://docs.python.org/3.5/library/string.html#formatstrings

这是脚本:

userinput = int(input("Enter the number of lines: " ))   # User input of the total number of lines
userinput = userinput + 1   # adding a value of 1 additionally with the user input to make numbers even

for i in range(1, userinput):   # Loop through lines from 1 to userinput

    for j in range(userinput - i):  # printing spaces, 1 at a time from j = 1 to j = userinput - i 
        print(" ", end = " ")

    for j in range(i, 0, -1):  # printing number decreasing from the line number j to 1
        print(j, end = " ")

    for j in range(2,i + 1):   # Printing number increasing from 2 to line number j
        print(j, end = " ")
    print()
    j += 1  

小于10时的输出

Enter the number of lines: 9
                  1 
                2 1 2 
              3 2 1 2 3 
            4 3 2 1 2 3 4 
          5 4 3 2 1 2 3 4 5 
        6 5 4 3 2 1 2 3 4 5 6 
      7 6 5 4 3 2 1 2 3 4 5 6 7 
    8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 
  9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 

15以上时的输出:

Enter the number of lines: 15
                              1 
                            2 1 2 
                          3 2 1 2 3 
                        4 3 2 1 2 3 4 
                      5 4 3 2 1 2 3 4 5 
                    6 5 4 3 2 1 2 3 4 5 6 
                  7 6 5 4 3 2 1 2 3 4 5 6 7 
                8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 
              9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 
            10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 
          11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 
        12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 
      13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 
    14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
  15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

当我为 10 人及以上预留了额外的空间时,我的输出如下所示:(这些点用于区分空白空间,我所做的只是在打印的开头添加了 " " 引号.

Enter the number of lines: 12
. . . . . . . . . . . .   1                        
. . . . . . . . . . .   2  1  2                      
. . . . . . . . . .   3  2  1  2  3                    
. . . . . . . . .   4  3  2  1  2  3  4                  
. . . . . . . .   5  4  3  2  1  2  3  4  5                
. . . . . . .   6  5  4  3  2  1  2  3  4  5  6              
. . . . . .   7  6  5  4  3  2  1  2  3  4  5  6  7            
. . . . .   8  7  6  5  4  3  2  1  2  3  4  5  6  7  8          
. . . .   9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9        
. . .   10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10      
. .   11  10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10  11    
.   12  11  10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  10  11  12  

这是我尝试通过添加额外空间来更改的内容

  for j in range(userinput - i):  # printing spaces, 1 at a time from j = 1 to j = userinput - i 
        print(".", end = " ")

    for j in range(i, 0, -1):  # printing number decreasing from the line number j to 1
        print(" ", j, end = "")

    for j in range(2,i + 1):   # Printing number increasing from 2 to line number j
        print(" ", j, end = "")

    for j in range(userinput - i):  # printing spaces, 1 at a time from j = 1 to j = userinput - i 
        print(" ", end = " ")

这是我想要完成的理想输出:

                                           1 
                                        2  1  2 
                                     3  2  1  2  3 
                                  4  3  2  1  2  3  4 
                               5  4  3  2  1  2  3  4  5 
                            6  5  4  3  2  1  2  3  4  5  6 
                         7  6  5  4  3  2  1  2  3  4  5  6  7 
                      8  7  6  5  4  3  2  1  2  3  4  5  6  7  8 
                   9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 
               10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 
            11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 
         12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 
      13 12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 13 
   14 13 12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 13 14 
15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 

谢谢!

【问题讨论】:

    标签: loops python-3.x for-loop formatting string-formatting


    【解决方案1】:

    这个问题要考虑的事情是

    • 最大数字的长度。
    • 正在打印的当前号码的长度。
    • 长度不同。

    为了正确间隔所有内容,您需要打印额外的 数字较少的数字后面的空格(以补偿较大数字中的额外数字)。

    例如,如果您有一行包含数字 10,为了正确分隔其他较小的数字,您将需要使用额外的空格来补偿数字 10 中的第二个数字。

    这个解决方案对我有用。

    userinput = int(input("Enter the number of lines: " ))
    userinput = userinput + 1   
    
    # Here, you can see I am storing the length of the largest number
    input_length = len(str(userinput))
    
    for i in range(1, userinput):
    
        # First the row is positioned as needed with the correct number of spaces
        spaces = " " * input_length
        for j in range(userinput - i): 
            print(spaces, end = " ")
    
        for j in range(i, 0, -1):
            # Now, the current numbers length is compared to the 
            # largest number's length, and the appropriate number
            # of spaces are appended after the number.
            spaces = " " * (input_length + 1 - len(str(j)))
            print(j, end = spaces)
    
        for j in range(2,i + 1):
            # The same is done here as in the previous loop.
            spaces = " " * (input_length + 1 - len(str(j)))
            print(j, end = spaces)
        print()
        j += 1  
    

    【讨论】:

    • 谢谢!如果您有时间添加更多解释,那就太好了,因为这就是我想要了解的更多内容。我会看一下尝试理解。
    • @Dmitriy,我已经用解释更新了我的答案。我希望它有所帮助。
    • 感谢您的解释。我想要理解的一件事是 spaces = " " * (input_length + 1 - len(str(j))) 所以如果我理解正确,当有两位数时,它在做什么是 " " * (2 + 1 - 1) ?因为j 在技术上也是1?我试图了解我们如何计算背后的逻辑空间。谢谢!
    • 没错。此解决方案也适用于具有更多位数的较大数字。
    • 感谢您的解释,我不知道函数 len(str),它使它变得如此简单。很高兴知道!
    【解决方案2】:

    看看 https://stackoverflow.com/a/13077777/6510412

    我认为这可能是您正在寻找的。希望对你有帮助。

    【讨论】:

    • 我正在尝试用我的代码来完成它并应用到它,但是在使用 codeformat(" ", 4d)code 或其他格式时,我无法获得格式化工作,它会破坏程序。只是想修复我的。谢谢!
    • 对不起,我的意思是告诉你这个答案stackoverflow.com/a/13077777/6510412我已经更新了我原来的答案。
    • 感谢您的链接。我正在寻找更多的解释,因为仅通过查看某人编写的代码很难分辨。我正在尝试弄清楚并学习如何使用字符串格式。
    • 我明白了。好吧,这个问题要考虑的事情是最大数字的长度和正在打印的当前数字的长度。为了正确间隔所有内容,您需要在数字较少的数字之后打印额外的空格,以补偿额外的长度。例如,如果您有一行包含数字 10,为了正确分隔其他较小的数字,您将需要使用额外的空格来补偿数字 10 中的第二个数字。我希望这会有所帮助。
    • 这正是我认为我会做的事情。我试过在一个额外的空间里放一个带引号的打印,这只是弄乱了整个格式,我想知道我的代码在哪里犯了错误。在我的 3 个 for 循环中,我有空格,当我添加额外的空间时,这不起作用,这在逻辑上没有意义,为什么它不起作用。谢谢!
    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 2015-04-05
    • 2013-07-06
    • 2018-03-30
    • 2020-03-26
    相关资源
    最近更新 更多