Tabs in print are not consistent python

后端 未结 5 1146
庸人自扰
庸人自扰 2021-01-11 12:54

I want to have real tabs in a print but \\t only puts spaces. Eg:

 first:ThisIsLong         second:Short
 first:Short         second:ThisIsLong
         


        
相关标签:
5条回答
  • 2021-01-11 13:19

    You can use formatting to align the strings instead. For example, you can tell python that the first column should be 20 characters long, and the second should be 10, left aligned.

    For example:

    string_one = 'first:ThisIsLong'
    string_two = 'second:Short'
    
    print( '{:<20s} {:<10s}'.format(string_one, string_two) )
    

    will print:

    first:ThisIsLong     second:Short
    

    The first formatting descriptor ({:<20s}) here is saying:

    '<' left align, 20 at least 20 characters, s because it's a string

    0 讨论(0)
  • 2021-01-11 13:33

    The Python print function sends strings to standard output. What standard output does with those strings depends on the output device. The default interpretation of \t is to advance to the next tab stop, which by convention is the character position which is the next multiple of 8 after the position in which \t occurs (counting character positions from the left beginning with 0).

    For example, if I run:

    babynames = [('kangaroo', 'joey'), ('fox', 'kit'), ('goose','gosling')]
    for x,y in babynames: print(x + '\t' + y)
    

    I get:

    kangaroo        joey
    fox     kit
    goose   gosling
    

    I got the above in IDLE. kangaroo occupies columns 0-7. \t is in column 8, hence the next multiple of 8 (the next tab stop) after the tab is in column 16 -- which is indeed where you see joey. In the next two lines -- the next tab stop after the first word is in column 8. This shows that (at least in the IDLE shell) Python is using real tabs.

    Tabs in this sense are somewhat annoying. They can only be used to align variable-length string data with a certain amount of annoying difficulty. As others have indicated the solution is to not use tabs but instead use format

    0 讨论(0)
  • 2021-01-11 13:40

    The most Pythonic way to do this is:

    string_one = 'first:ThisIsLong'
    string_two = 'second:Short'
    
    print(f'{string_one:<20s} {string_two:<10s}')
    

    You can use an F-string (format string) to align the strings instead. For example, you can tell python that the first column should be 20 characters long, and the second should be 10, left aligned.

    using this F-string will print:

    first:ThisIsLong     second:Short
    

    Broken Down

    f'{insert_variable_here:<20s}' says:

    : take all before this and format as: < left align, 20 at least 20 characters, s because it's a string.

    Credit to mjwunderlich for the format.

    0 讨论(0)
  • 2021-01-11 13:44

    Instead of using tab (\t), I suggest to use string formatting using printf-style formatting or str.format:

    rows = [
        ['first:ThisIsLong', 'second:Short'],
        ['first:Short', 'second:ThisIsLong'],
        ['first:ThisIsEvenLonger', 'second:Short'],
    ]
    for first, second in rows:
        print('%-25s %s' % (first, second))
    

    or

    print('{:<25} {}'.format(first, second))
    
    0 讨论(0)
  • 2021-01-11 13:44

    Compute the maximum width needed for each column and then use string formatting to compose a format which specifies the desired width:

    data = [['first:ThisIsLong', 'second:Short'],
            ['first:Short', 'second:ThisIsLong'],
            ['first:ThisIsEvenLonger', 'second:Short']]
    
    widths = [max([len(item) for item in col]) for col in zip(*data)]
    fmt = ''.join(['{{:{}}}'.format(width+4) for width in widths])
    for row in data:
        print(fmt.format(*row))
    

    yields

    first:ThisIsLong          second:Short         
    first:Short               second:ThisIsLong    
    first:ThisIsEvenLonger    second:Short         
    
    0 讨论(0)
提交回复
热议问题