I'm required to covert the variable:
pi_string = "3.1415926"
into a float. Here's what I'm dealing with:
We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.
The atof() function returns a double value that is produced by interpreting the input characters as a number. The return value is 0 if the function cannot convert the input to a value of that type.
Your line should be pi_float = float(pi_string)
float(pi_string)
is a float value, you can not assign to it, because it is not a variable.
The method float()
will do this for you if pi_string = "3.1415926"
.
>>>pi_float = float(pi_string)
>>>type(pi_float)
<class 'float'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With