# extracting numbers with decimal places from a string

Joel Goldstick joel.goldstick at gmail.com
Sun Jan 11 22:42:57 CET 2015

```On Sun, Jan 11, 2015 at 3:26 PM, Store Makhzan <store0a at gmail.com> wrote:
> On Sunday, January 11, 2015 at 2:06:54 PM UTC-6, Joel Goldstick wrote:
>> On Sun, Jan 11, 2015 at 2:31 PM, Store Makhzan wrote:
>> > I have this script which can calculate the total of numbers given in a string
>> > ---- script -----
>> > total = 0
>> > for c in '0123456789':
>> >    total += int(c)
>> > print total
>> > ---- script -----
>> >
>> > How should I modify this script to find the total of if the numbers given in the string form have decimal places? That is, how do I need to modify this line:
>> > ----- script -----
>> > for c in '1.32, 5.32, 4.4, 3.78':
>> > ----- script -----
>> > to find the total of these given numbers.
>> > --
>> > https://mail.python.org/mailman/listinfo/python-list
>>
>> split the string on ',' to get a list of strings.  loop thru list
>> using float(s), add float values
>>
>>
>> --
>> Joel Goldstick
>> http://joelgoldstick.com
>
> Thank you
> Here is what I did:
>
> ----- script -----
> #Check if a perfect cube
> total = 0
> for c in ('1.23', '2.4', '3.123'):
>    print float(c)
>    total += float(c)
> print total
> ----- script -----
>
> Which gave me the result I wanted.
> Since I am new to Python, I used () as a guess, and it worked.

That's fine, but its different than your original question.  In your
original question you had a string of floats separated by commas.  To
solve that problem you need to first split the string on the commas:

my_list = "1.23, 2.4, 3.123".split(",")

that will give you ['1.23', '2.4', '3.123']

>From there do what you did starting with your for loop

> --
> https://mail.python.org/mailman/listinfo/python-list

--
Joel Goldstick
http://joelgoldstick.com

```