Convert String to Float in Python

  • Post category:String

In Python, you can use a float() function to convert a string to float. In the below code, we will convert a string to a float. The float() function takes only one argument. The argument is the string to be converted.

Step 1: Create a string

str = "123.45"
print("str = ", str)
print("str type = ", type(str))

Step 2: Convert string to float

print("str = ", float(str))
print("str type = ", type(float(str)))

Output:

str =  123.45
str type =  <class 'str'>
str =  123.45
str type =  <class 'float'>

Free resources to learn advanced skills: AiHints and CodeAllow