如何使用大块字符串在Python中打印格式? [英] How to do print formatting in Python with chunks of strings?

查看:145
本文介绍了如何使用大块字符串在Python中打印格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在格式化金字塔时遇到了一些麻烦。我试图从循环打印时使用格式,但似乎并没有工作,只是打破了程序。什么是不同的方式来格式化输出。我遇到的唯一的麻烦是当我打印10以上时,有两位数字。格式化打印输出的最佳方法是什么?我已经尝试了各种各样的方法,但无法使格式化工作从文档
https://docs.python.org/3.5/library/string.html#formatstrings



以下是脚本:

  userinput = int(input(输入行数:))#用户输入的总行数
userinput = userinput + 1#在用户输入的基础上增加一个值1,使得数字甚至为

(i,范围为1,userinput):#从1循环到userinput

for j in range(userinput - i):#打印空格,每次从j = 1到j = userinput 1 - 1
print(,end =)
(i,0,-1)中的
:#从行号j减少到1的b印刷数b $ b print(j,end =)

j在范围(2,i + 1)中:#打印数从2增加到行号j
print(j,end =)
print()
j + = 1

小于10时的输出

 输入行数: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以上的输出:

 输入行数: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以上的空间保留了一个额外的空间时,这里是我outout看起来像:(点用来区分空的空间,我所做的是添加了一个<$ c $

 输入行数: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

这里是我试图通过添加aditional space

 来改变范围(userinput  -  i)中的j:#printing空格,从j = 1到j = userinput  -  i 
print(。,end =)

(对于范围(i,0,-1) ):#从行号j减少到1
print(,j,end =)

在范围(2,i + 1)打印数量从2增加到行号j
print(,j,end =)

在范围内(userinput-i):#打印空格,1从j = 1到j = userinput的时间 -
print(,end =)



$ p
$ b

  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的行,为了正确地分隔其他较小的数字,您将需要使用额外的空格补偿数字10中的第二位数字。



    这个解决方案适用于我。

      userinput = int(input(输入行数:))
    userinput = userinput + 1

    #在这里,你可以看到我存储的长度(1,userinput)中的最大数字
    input_length = len(str(userinput))



    #首先根据需要定位行(b,b,b,b,b,b,b,b)b

    $ b $空格=* input_length
    在范围内(userinput -i) b对于范围(i,0,-1)中的j:
    #现在,将当前数字长度与
    #最大数字长度进行比较,并且相应的数字
    #附在数字后面。 (2,i)中的
    空格=*(input_length + 1 - len(str(j)))
    print(j,end = spaces)

    + 1):
    #这里和前面的循环一样。
    空格=*(input_length + 1 - len(str(j)))
    print(j,end = spaces)
    print()
    j + = 1


    I'm having some trouble with formatting the pyramid. I've tried to use format when printing from the loop but that didn't seem to work and just breaks the program. What would be different ways to format the output. The only trouble that I am having is when I am printing 10 and up when there's double digits. What would be the best approach formatting the printing output? I've tried variety of ways but couldn't make formatting work within the loop from documentation https://docs.python.org/3.5/library/string.html#formatstrings

    Here is the script:

    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  
    

    The output when its less than 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 
    

    The output when it's 15 or more:

    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 
    

    When I have reserved an extra space for 10 and up, here is what my outout looks like: (The dots were used to distinguish from empty space, all I did was added a " " quotes in the beginning of the print.

    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  
    

    Here is what I've tried changing by adding aditional space

      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 = " ")
    

    Here is the ideal output of what I am trying to accomplish:

                                               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 
    

    Thank you!

    解决方案

    The things to consider for this problem are

    • The length of the largest number.
    • The length of the current number being printed.
    • The difference in lengths.

    In order to correctly space everything, you're going to need to print extra spaces after the numbers with less digits (to compensate for the extra digits in the larger number).

    For example, if you have a row that contains the number 10, in order to correctly space the other smaller numbers, you're going to need to use extra spaces to compensate for that second digit in the number 10.

    This solution works for me.

    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  
    

    这篇关于如何使用大块字符串在Python中打印格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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