OneBite.Dev - Coding blog in a bite size

convert variable from string to float in python

Code snippet on how to convert variable from string to float in python

  num_string = "19.85"
  num_float = float(num_string)

In this code snippet, we are using Python’s built-in float() function to convert a string to a float. The string “19.85” is assigned to the variable num_string. Then, the float() function is called with num_string as the argument. The returned float is assigned to the variable num_float. The num_float variable now stores the float 19.85.

python