Cim Took
Cim Took

Reputation: 153

Concatenating \t python gives irregular spacing

w.write(str(entries[0]) + "\t" + (str(entries[1])) + "\n")

This gives the output as

Zygophyseter    1
Zygorhiza       1
Zygospore       1
Zygote_(band)   1
Zygote_intrafallopian_transfer  1
Zygotes 1
Zygovisti       1

Why is there so irregular spacing in different lines between name and the number 1

Upvotes: 0

Views: 679

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123940

You have misunderstood what tabs mean. A tab character means: move to the next tab stop from the current position.

Where those tab stops are located depends on your text editor or terminal. Usually they are located at every 8th column, Stack Overflow tabs these to the fourth column instead:

>>> def show_tabs():
...     print('\t'.join(['v'] * 8))
...     for i in range(1, 33):
...         print(''.join(map(lambda j: str(j)[-1], range(1, i))), 'tab to here', sep='\t')
... 
>>> show_tabs()
v   v   v   v   v   v   v   v
    tab to here
1   tab to here
12  tab to here
123 tab to here
1234    tab to here
12345   tab to here
123456  tab to here
1234567 tab to here
12345678    tab to here
123456789   tab to here
1234567890  tab to here
12345678901 tab to here
123456789012    tab to here
1234567890123   tab to here
12345678901234  tab to here
123456789012345 tab to here
1234567890123456    tab to here
12345678901234567   tab to here
123456789012345678  tab to here
1234567890123456789 tab to here
12345678901234567890    tab to here
123456789012345678901   tab to here
1234567890123456789012  tab to here
12345678901234567890123 tab to here
123456789012345678901234    tab to here
1234567890123456789012345   tab to here
12345678901234567890123456  tab to here
123456789012345678901234567 tab to here
1234567890123456789012345678    tab to here
12345678901234567890123456789   tab to here
123456789012345678901234567890  tab to here
1234567890123456789012345678901 tab to here

Running the above code in your own terminal will quickly reveal what your tab setting is.

In your text, you have one line that is shorter than 8 characters, so the next tab stop is at the 8 characters from the start. One line is longer than 16 characters, so the next tab stop is at 24 characters. The majority of lines contains between 9 and 14 characters, so the next tab stop is at 16 characters. In other words, your output is entirely consistent with using a tab character between the columns.

If you wanted to produce text output that contains perfectly aligned columns, either adjust your tabs to fit a specific tab-stop configuration (use more or fewer tabs depending on the length of the first column), or use spaces only, and again adjust the spacing. Python can help with the latter, see formatting with str.format(), you can left or right align text in a given width of whitespace.

Upvotes: 5

Related Questions