Albatros
Albatros

Reputation: 23

Python Output not Aligned Using Space and Tab

My code is here:

days = int(input("How many days did you work? : "))
totalSalary = 0
print("Day", "\tDaily Salary", "\tTotal Salary")
for day in range(days):
    daily = 2**day
    totalSalary += daily
    print(day+1, "\t    ", daily, "\t\t    ", totalSalary)

When I enter 6 as input, here is the output:

Day     Daily Salary    Total Salary
1            1               1
2            2               3
3            4               7
4            8               15
5            16                      31
6            32                      63

Why last 2 lines are not aligned?

Edit: I forgot to say that I know there are better solutions like using format, but I just wanted to understand why there is problem with tabs and spaces.

Edit2: The visualization of tabstops in Jason Yang's answer satisfied me.

Upvotes: 0

Views: 969

Answers (6)

Mazhar
Mazhar

Reputation: 1064

Dynamic space declaration. Due to length of the digit

days = int(input("How many days did you work? : "))
totalSalary = 0
day_data = []
for day in range(days):
   daily = 2**day
   totalSalary += daily
   day_data.append([day+1,daily,totalSalary])
   
num_space = len(str(day_data[-1][-1]))+2
f_space_len, s_space_len = 5+num_space, 9+num_space
print(f"Day{num_space*' '}Daily Salary{num_space*' '}Total Salary")
for i in day_data:
   day, daily, totalSalary = map(str, i)
   print(day, (f_space_len-len(day)+1)*' ', daily,(s_space_len-len(daily)+1)*' ', totalSalary)

enter image description here

Upvotes: 0

Bertal
Bertal

Reputation: 1

I think that you added too much spaces in " print(day+1, "\t ", daily, "\t\t ", totalSalary)". when you remove the tab spaces you will not get the "not aligned" problem.

Upvotes: 0

Jason Yang
Jason Yang

Reputation: 13061

For statement

    print(day+1, "\t    ", daily, "\t\t    ", totalSalary)

each '\t' will stop at 1, 9, 17, ..., at each 8th character

So it will look like this

1=------____=1=-........____=1
2=------____=2=-........____=3
3=------____=4=-........____=7
4=------____=8=-........____=15
5=------____=16=--------........=31
6=------____=32=--------........=63
12345678123456781234567812345678 <--- Tab stop before each 1

Here

  • = is the separator space between each two arguments of print
  • - is the space generated by not-last TAB
  • _ is the space specified by you in your print.
  • . is the sapce generated by last TAB.

From here you can find the differece why they stop at different position.

Try to add option sep='' in your print, or change the numbers of spaces you added.

    print(day+1, "\t    ", daily, "\t\t    ", totalSalary, sep='')

then it will be fine.

How many days did you work? : 6
Day     Daily Salary    Total Salary
1           1               1
2           2               3
3           4               7
4           8               15
5           16              31
6           32              63

Upvotes: 1

tozCSS
tozCSS

Reputation: 6114

I suppose your question is due to a misunderstanding on what a tab character is and how it behaves:

A tab character should advance to the next tab stop. Historically tab stops were every 8th character, although smaller values are in common use today and most editors can be configured. Source: How many spaces for tab character(\t)?

try this and see:

print('123456789')
print('1\t1')
print('12\t1')
print('123\t1')

enter image description here

Upvotes: 0

chl
chl

Reputation: 385

tab is not just collection of white spaces

Any character before tab fills the space.

>>> print("A\tZ")
A       Z
>>> print("AB\tZ")
AB      Z
>>> print("ABC\tZ")
ABC     Z

and if there are no spaces to fill tab, then it will be shifted

>>> print("ABCDEFGH\tZ")
ABCDEFGH        Z

Upvotes: 0

Anand Sowmithiran
Anand Sowmithiran

Reputation: 2920

When the 2nd columns has double digit values, the rest of the tab and 3rd column gets shifted. You must use zero padding for the values, if you expect correctly aligned column values.

If your python version is 3+, and if you want 2 digit values, you can call print(f'{n:02}'), so as to print 01 if the value of n was having 1.

For 2.7+ version of python, you could use the format like so print('{:02d}'.format(n)).

Upvotes: 0

Related Questions