Steve P.
Steve P.

Reputation: 14709

Seeminlgy odd behavior in print statement for multiple tabs

I have looked this up and can't seem to figure out why it's happening.

print "Percentage of A\tB\tC\tD"
#prints: Percentage of A B      C        D

However,

print "Percentage of  A\tB\tC\tD"
#prints: Percentage of  A        B       C        D

Two questions:

  1. Why does the additional space between of and A make a difference?
  2. Why aren't the space intervals between the letters equivalent in either of the prints?

Upvotes: 0

Views: 47

Answers (2)

user3053230
user3053230

Reputation: 199

Alternatively, you could use string formatting and specify the width of each field (e.g. {0:8} {1:8} etc)

Upvotes: 4

Martijn Pieters
Martijn Pieters

Reputation: 1125068

How tabs are handled depends on the terminal or console. Often, they indent to specific tab stops; predefined columns regardless of where the tab was printed.

E.g. if there are tab stops every 8 spaces, printing a tab at column 12 means the cursor skips to column 16, and does not skip 8 spaces to column 20.

With the addition of a space you made the tab fill out to the next tab stop at position 24:

>>> len('Percentage of A')
15
>>> len('Percentage of  A')
16

Upvotes: 2

Related Questions