yyin
yyin

Reputation: 71

Python's tab spacing

So I have a program that prints out two "fish" per line, and the distance between two fish is a tab character "\t", the output looks like this:

enter image description here

My question is, you see, for example, Theodore is longer than Marge, and they all have a "\t" character after them. So why is Simon and Maggie aligned? And why are Homer and Maude the position they are at? I'm suspecting that there are some invisible gridding? I'm using pycharm

Thank you very much!

Upvotes: 0

Views: 6504

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121734

I'm suspecting that there are some invisible gridding?

That's exactly what tabs do, move the cursor position to the next available tab stop, which are basically gridlines. Tabstops are usually given every 4th or 8th column.

So when you are printing the \t tab at column 5 in a 8th-column tab stop configuration, the next tab stop column would be at position 8. But if your text so far brought you to column 8, then the next stop would be at column 16. Where the next set of characters are printed depends then on how far the preceding text has taken you so far.

Don't use tabs if you need exact control over the output columns. Use string formatting with minimal field sizes to format your data.

'<>< {:30} <>< {:30}'.format(
    '({}, {})'.format(vara, varb),
    '({}, {})'.format(varc, vard))

Now the output string will take at least 30 characters for the each field, adding spaces to make up the difference.

Upvotes: 4

Related Questions