0

I want to print"After tax, your total is: $8.74999125." no whitespace after $ sign.

how could I do this in this statement?

print("After tax, your total is: $",total_price). 

The output of this statement adds whitespace after $.

total price is type float.

aguy01
  • 13
  • 1
  • 3
  • 7

3 Answers3

1

I would use string formatting

print("After tax, your total is: ${}".format(total_price))
flazzarini
  • 7,791
  • 5
  • 33
  • 34
1

Convert total_price to string, then use string concatenations:

print("After tax, your total is: $" + str(total_price))

Or use strings formatting:

print("After tax, your total is: ${}".format(total_price))
Uriel
  • 15,579
  • 6
  • 25
  • 46
  • Great! Thank you. I am wondering why the leading space is added at the output if string concatenation is not used. – aguy01 Oct 25 '16 at 22:13
  • The print function convert each separate argument to string and joins them by spaces. When using concatenation you pass only one argument that you design. – Uriel Oct 25 '16 at 22:32
  • The space, which separates print arguments, can be changed using sep keyword parameter (see my answer below). – Andrew Oct 25 '16 at 22:43
1

Third variant: it is also possible to change separator symbol (to empty string in current case):

print("After tax, your total is: $",total_price, sep=''). 
Andrew
  • 720
  • 1
  • 6
  • 9