Tabs in print don't match python

I want to have real tabs in print, but \t only puts spaces. For instance:

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

How can I fix this so that I have all the first and all built seconds. For instance:

  first:ThisIsLong second:Short first:Short second:ThisIsLong first:ThisIsEvenLonger second:Short 
+5
source share
4 answers

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

For instance:

 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 says:

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

+5
source

Instead of using the tab ( \t ), I suggest using formatting strings using formatting in printf 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)) 
+3
source

Calculate the maximum width required for each column, and then use row formatting to compose a format that sets the required 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)) 

gives

 first:ThisIsLong second:Short first:Short second:ThisIsLong first:ThisIsEvenLonger second:Short 
+3
source

The Python print function sends strings to standard output. Which standard output with these lines depends on the output device. The default interpretation of \t is to move to the next tab, which by convention represents the position of the character, which is the next multiple of 8 after the position in which \t occurs (counting the positions of characters to the left of 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 above in IDLE. kangaroo takes columns 0-7. \t is in column 8, so the next multiple is 8 (next tab) after the tab is in column 16, which is really where you see joey . On 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 uses real tabs.

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

+3
source

Source: https://habr.com/ru/post/1240395/


All Articles