Python Strings: Medical Insurance Project Part 22 Extra

So I’ve been attempting to do the extra assignment part 22 on the Python Strings: Medical Insurance Project on Jupyter. Im attempting to remove the “$” from my insurance cost, however, its not processing through which in turn is not allowing me to calculate to total insurance cost which stops me from getting the average. Whenever I attempt to strip it, it tells me that the “List Object has no attribute to strip.”

I’m just seeking some assistance in stripping the dollar symbol from the code so that I can move forward with getting the average_insurance_costs

If you have something like this

costs = ["$342", "$156", "$205"]

and you’d like to take out the $ and turn the strings into numbers (floats)
then you could do loop
or a list comprehension, like this

costs = [ float(amount.strip('$')) for amount in costs]

or you could do each one individually

costs[0] = float(costs[0].strip('$'))
costs[1] = float(costs[1].strip('$'))
costs[2] = float(costs[2].strip('$'))

or you could do that with a loop.

1 Like

Hello

Here are the steps to remove the dollar signs from the insurance cost list.

  1. Create an empty list called new_insurance_costs
new_insurance_costs = []
  1. Create a for loop and strip() method to each cost and append it to the list.
for cost in insurance_costs:
  new_insurance_costs.append(cost.strip("$"))
  1. Print the list to confirm that dollar sign has been removed.
print("Insurance costs without dollar sign: " + str(new_insurance_costs))

Hope this helps!

:+1:

2 Likes