Python: Extract multiple float numbers from string
<p>Forgive me, I'm new to Python.</p>

<p>Given a string that starts with a float of indeterminate length and ends with the same, how can I extract both of them into an array, or if there is just one float, just the one.</p>

<p>Example: </p>

<pre><code>"38.00,SALE ,15.20"
"69.99"
</code></pre>

<p>I'd like to return:</p>

<pre><code>[38.00, 15.20]
[69.99]
</code></pre>

<p>You said you're only interested in floats at the start and end of the string, so assuming it's comma-delimited:</p>

<pre><code>items = the_string.split(',')
try:
    first = float(items[0])
except (ValueError, IndexError):
    pass
try:
    second = float(items[-1])
except (ValueError, IndexError):
    pass
</code></pre>

<p>We have to wrap the operations in exception handlers since the value might not be a valid float (<code>ValueError</code>) or the index might not exist in the <code>list</code> (an <code>IndexError</code>).</p>

<p>This will handle all cases including if one or both of the floats is omitted.</p>

python string