13
votes

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

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

How would i fix it so i can have all the firsts and all the seconds lined up. Eg:

 first:ThisIsLong         second:Short
 first:Short              second:ThisIsLong
 first:ThisIsEvenLonger   second:Short
5
sorry \t not \n im new here still getting used to stack overflow - ewan
The sort of thing that you show is the sort of thing that you expect with real tabs - John Coleman
real tabs snap to a point - ewan
But -- not always to the point that you expect. See my answer. - John Coleman

5 Answers

18
votes

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

8
votes

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))
5
votes

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         
5
votes

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

1
votes

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.