Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tabs in print are not consistent python

Tags:

python

string

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
like image 821
ewan Avatar asked Jan 12 '16 02:01

ewan


People also ask

What does \t mean in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

How do you print tab space in Python?

The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t' . To see the tab spaced character in the REPL wrap any variable containing a tab character in the built-in print() function.

How do you count tabs in Python?

Finding the indices of the spaces tell you how many there are so that you can calculate how many tabs there are - a tab is N spaces as defined by the user. You don't need to find the indices to count them; num_spaces = elem. count(' ') works fine.


3 Answers

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

like image 74
mjwunderlich Avatar answered Oct 13 '22 09:10

mjwunderlich


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))
like image 9
falsetru Avatar answered Oct 13 '22 11:10

falsetru


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

like image 6
John Coleman Avatar answered Oct 13 '22 09:10

John Coleman