-1

Im currently writing a program that involves money, and I want my output to have a dollar sign before the output but for some reason python insists on putting a space before my output which will not work for me, so im curious if there is any way around this.

amount = int(input("please enter the overall ammount: "))
full_shares = int(input("please enter the number of full shares: "))
print(" ")
half_shares = int(input("please enter the number of halfshares: "))
print(" ")

def split_bill(amount, full_shares, half_shares = 0):
    
    full_share_ammount = amount/full_shares
    half_share_ammount = full_share_ammount/2
    print("Full share: $", full_share_ammount)
    print("Half share: ", half_share_ammount)


split_bill(amount, full_shares, half_shares)

output:

please enter the overall ammount: 1000
please enter the number of full shares: 4
 
please enter the number of halfshares: 4
 
Full share: $ 250.0
Half share:  125.0
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • 1
    sorry my title didnt really explain my issue, im trying to add a '$' before the output of my program, so for example if my output was 4 I would want it to be $4 if that makes any sense at all. – Nathan Lockhart Mar 13 '23 at 02:43

3 Answers3

4

There are better ways to combine strings, but if you want to use the print function this way, you can provide the sep argument with an empty string (it defaults to a single space).

https://docs.python.org/3/library/functions.html#print

>>> print("Full Share $", full_share_amount, sep="")
Full Share $250.0
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
3

Alternatively you can use an f-string. This is a way to format variable values in a string:

print(f'Full Share ${full_share_amount}")
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

You could use format method:

print("Full share: ${}".format(full_share_amount))

The {} is a placeholder. You can read more about it here

Marcelo Paco
  • 2,732
  • 4
  • 9
  • 26