My issue: I’d like to add all the digits in this string '1.14,2.14,3.14,4.14'
but the commas are causing my sum function to not work correctly.
I figured using a strip function would solve my issue but it seems as though there is still something I’m missing or not quite understanding.
JavaScript
x
5
1
total = 0
2
for c in '1.14,2.14,3.14'.strip(","):
3
total = total + float(c)
4
print total
5
I have searched how to remove commas from a string but I only found information on how to remove commas from the beginning or ending of a string.
Additional Info: Python 2.7
Advertisement
Answer
I would use the following:
JavaScript
1
6
1
# Get an array of numbers
2
numbers = map(float, '1,2,3,4'.split(','))
3
4
# Now get the sum
5
total = sum(numbers)
6