cheese510
cheese510

Reputation: 81

Adding $ infront of variable within print statement in python 3.3.4

I have the following statement:

print ("You have entered the price of ",var1, "for a chocolate bar")

Which works happily however i need to put a '$' infront of it for the output. I tried hiding it in the print statement however then you end up with a gap between it so you end up with

'You have entered the price of $ 34.33 for a chocolate bar'.

I need it to be:

'You have entered the price of $34.33 for a chocolate bar'.

I am very new to python, and this is tripping me up. Any help would be appreciated. Im using python 3.3.4

Upvotes: 1

Views: 67

Answers (2)

aelor
aelor

Reputation: 11126

print "You have entered the price of ","$"+str(var1), "for a chocolate bar"

Upvotes: 1

Jayanth Koushik
Jayanth Koushik

Reputation: 9904

format is the recommended way to do this.

print("You have entered the price of ${0} for a chocolate bar".format(var1))

Upvotes: 4

Related Questions