Sal's Shipping Project - How do I display the costs as dollars and cents?

Very new, trying to wrap my head around things. This seems like something someone would have asked, but I can’t find another thread asking this particular question. Working through this project and it seems pretty straightforward so far as the requirements ask of you, but I want to experiment a little bit.

The costs to ship a package are displayed with only one decimal spot. Being that this is supposed to be currency, it seems appropriate to have two decimals.

Here’s a portion of the project for reference:

cost_premium = 125.00
print("Cost to ship via Ground Premium is $", cost_premium)

This results in it printing the cost as 125.0.

In this case, how would I display the result as “125.00” instead of just “125.0”?

Additionally, is there a better way to include a dollar sign? As it is here in my example, there’s a space between the $ sign and the 125.0. Like this:

Cost to ship via Ground Premium is $ 125.0

Thanks in advance.

https://www.codecademy.com/courses/learn-python-3/projects/python-sals-shipping

You can use .format to change the number to a String and have a string version of the number [float] rounded to 2 decimal places (using {:.2f} in the string ).
For example

print( "{:.2f}".format(16.7892) )

would print the following to the screen:

16.79

So you could do

print("Cost to ship via Ground Premium is ${:.2f}".format(cost_premium) )

here’s one reference for the .format for strings in Python:

1 Like

Thank you. I had seen that through some Googling before posting this earlier, but wasn’t able to figure out how to actually use it.