Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The only problem is that you have to remove the comma. <code>4,534.07</code> is not a valid float literal, but <code>4534.07</code> is.</p> <p>(That's exactly what the <code>ValueError: invalid literal for float(): 4,534.07</code> is telling you, except that it's missing the "did you mean…?" suggestion.)</p> <p>So:</p> <pre><code>z='4,534.07' print float(z.replace(',', '')) </code></pre> <hr> <p>Also, all those attempts to "remove excess quotes" do nothing because there are no quotes in the string. Of course there are quotes when you print out the <code>repr</code> of the string, but that doesn't mean they're in the string itself, it means that the <code>repr</code> of any string is enclosed in an extra pair of quotes. Since those quotes aren't in the string, they can't influence any function you call on that string (unless that function does something really, really stupid, like calling <code>repr</code> on its argument to build up a string to call <code>eval</code> on…).</p> <p>Plus, even if the problem <em>were</em> excess quotes, just <code>print z[1:-1]</code> or <code>print z.replace("'", "")</code> wouldn't actually <em>remove them from <code>z</code></em>, it would just print out what it would look like if you had done so. To actually change the value of <code>z</code>, you have to assign something to it. For example, if you add <code>print z.replace(',', '')</code> to your existing code, the <code>float(z)</code> will still fail. But if you add <code>z = z.replace(',', '')</code>, then the <code>float(z)</code> will succeed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload