0

I am trying to add a dollar sign ("$") to the users input when it asks about their savings and deposits. At the end of my script I am creating a text file with all the information, but I want the numbers to have the symbol in front of it when the file is created.

savings =  int(input("How much money do you have in your savings: ")
deposits = int(input("How much money do you put in deposits: ") 

from tabulate import tabulate
table = tabulate([["Name", "Last", "Age", "Company", "Hourly Rate", "Occupation", "Savings", "Deposits"],
[(name), (last_name), (age), (company), (hourly_rate), (occupation), (savings, + "$"), (deposits)]], headers = "firstrow")

I've added + "$" to the savings variable, because I thought that would work but then I would get this error:

TypeError: bad operand type for unary +: 'str'

So in conclusion, I just want it to have the dollar symbol even when the text file is created because this is a sample of what it looks like for now:

Savings Deposits


9000 900 <----Missing Dollar Sign

I hope this makes sense. Thank you.

Amar Mujak
  • 15
  • 5

5 Answers5

2

See if you'll be using int then you cannot concatenate the money along with the dollar sign as '$' is a string. You can do it like this:

# Trying to make it have a special character when file is printed such as "$" Example: $2600
savings = '$' + input("How much money do you have in your savings: ")
deposits = '$' + input("How much money do you put in deposits: ")
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27
0

When you print the variables, you can type cast them into string and append $ at the start.

print("$"+str(savings))
print("$"+str(deposits))
George
  • 320
  • 3
  • 14
0

You could use f-strings.

If you just want to print with a $ in front then,

savings = int(input("How much money do you have in your savings: "))
deposits = int(input("How much money do you put in deposits: "))

print(f'Savings: ${savings}')
print(f'Deposits: ${deposits}')
Sample Output:

Savings: $51
Deposits: $25

If you want to save the savings and deposits with a $ in front, then

savings = '$' + input("How much money do you have in your savings: ")
deposits = '$'+ input("How much money do you put in deposits: ")

savings and deposits will now be strings and not int.

Ram
  • 4,724
  • 2
  • 14
  • 22
0

Well it's very easy todo

For Python3 you can simply use F Strings

savings = int(input("How much money do you have in your savings: "))
deposits = int(input("How much money do you put in deposits: "))
print(f"₹ {savings}")
print(f"₹ {deposits}")

Read More About F Strings Here

Aditya
  • 1,132
  • 1
  • 9
  • 28
0

You can use the % sign.
The % symbol is followed by a character pointing to the data type. If it is an integer, use d.

%s -> String
$d -> Int
%f -> Float
savings = int(input("How much money do you have in your savings: "))
deposits = int(input("How much money do you put in deposits: "))

savings = "$%d" % savings
deposits = "$%d" % deposits
Hailey
  • 302
  • 1
  • 7