Problems convert string into float


Hi there, im a beginner in coding and decided to start with python.

i have a problem in a course in Python: Python Objects

i have a list with prices:
list = [’$1.21’, ‘$4.50’, ‘$1.20’, ‘$3.57’ etc. ]

because of the dollar sign “$” each element is a string. But now, i have to strip the dollar sign and convert the string to a float. Finally i have to add all prices into total_sales.

i write the code:

total_sales = 0
for price in sales:
total_sales += price


i got a ValueError:
ValueError: could not convert string to float: ‘$1.21’

So i dont understand why my code is wrong and “price” cannot strip the dollar sign with .strip("$").

It would be nice if someone can help me.
Thank you


looking at the documentation:

we learn that strip returns a copy of the string with the character(s) removed. You do nothing with the returned result.


Thank you very much @stetim94

Now my code looks like:

for price in sales:
z = price.strip("$")
total_sales += float(z)

and it works. :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.