1

I have the following line of code

print("Income $ {:>8,d} pays Flat tax ${:>9.2f} and Current tax ${:>9.2f}".format(income,tax,curr_tax))

It gives me the output

Income $   10,000 pays Flat tax $     0.00 and Current tax $     0.00

But I am trying to join the $ sign with the numbers without any spaces in between. How do we do this with string.format()

Edit:

This is the output I am trying to achieve

Income    $50,000 pays Flat tax    $780.00 and Current tax      $0.00
Income     $10,00 pays Flat tax      $0.00 and Current tax      $0.00
Novice
  • 515
  • 7
  • 22

2 Answers2

1

You could format within your format

print("Income {:>8} pays Flat tax {:>9} and Current tax {:>9}".format('${:,d}'.format(income), '${:.2f}'.format(tax), '${:.2f}'.format(curr_tax)))

Which gives

Income  $10,000 pays Flat tax     $0.00 and Current tax     $0.00
Income  $50,000 pays Flat tax   $780.00 and Current tax     $0.00
aydow
  • 3,673
  • 2
  • 23
  • 40
0

@aydow has a solid answer.

Alternatively, use @student comment by changing > to <:

print("Income ${:<8,d} pays Flat tax ${:<9.2f} and Current tax ${:<9.2f}".format(10000,0.00,0.00))

Results in:

Income $10,000   pays Flat tax $0.00      and Current tax $0.00

Still leaves blanks after values but not sure if that bugs you.

paragbaxi
  • 3,965
  • 8
  • 44
  • 58