×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Parse a string to float in Python

Last Updated : April 20, 2025

To understand this example, you should know the following Python topics:

Given a string value (that contains float value) and we have to convert it into float value in Python.

Example:

Input:
str = "10.23"
print(float(str))
Output:
10.23

Input:
str = "1001"
print(float(str))
Output:
1001.0

Parsing a string to a float value

To parse a string value to a float value in Python, use the float() method, which is a library function in python, it is used to convert a given string or integer value to the float value.

Below is the syntax of float() method:

float(string_value/integer_value)

Example

In this example, we demonstrate how to use the float() function to convert string values to float values in Python:

# python code to demonstrate example of 
# float() function

str1 = "10.23"
str2 = "1001"

# printing str1 & str2 types
print("type of str1: ", type(str1))
print("type of str2: ", type(str2))

# converting to float value
val1 = float(str1)
val2 = float(str2)

# printing types and values of val1 & val2
print("type of val1: ", type(val1))
print("type of val2: ", type(val2))

print("val1 = ", val1)
print("val2 = ", val2)

The output of the above example is:

type of str1:  <class 'str'>
type of str2:  <class 'str'>
type of val1:  <class 'float'>
type of val2:  <class 'float'>
val1 =  10.23
val2 =  1001.0

Parsing a string input to the float

Generally, when we take input using the input() method, it returns a string value. To parse the string input to the float, use the input() method inside the float() method.

Remember, the input should be either a float or an int value.

Example

In this example, we demonstrate how to take user input and parse it as a float value in Python:

# Input and parse a float value

value = float(input("Input a float value : "))

# Printing the type and value
print("Type of the value is :", type(value))
print("And, the value is :", value)

The output of the above example is:

RUN 1:
Input a float value : 12.34
Type of the value is : <class 'float'>
And, the value is : 12.34

RUN 2:
Input a float value : 108
Type of the value is : <class 'float'>
And, the value is : 108.0

RUN 3:
Input a float value : 108Hello
Traceback (most recent call last):
  File "/home/main.py", line 3, in <module>
    value = float(input("Input a float value : "))
ValueError: could not convert string to float: '108Hello'

Python Parsing String to Float Exercise

Select the correct option to complete each statement about parsing a string to a float in Python.

  1. To parse a string to a float in Python, use the ___ function.
  2. The correct way to parse the string "3.14159" to a float is ___.
  3. If you try to parse the string "hello" to a float using the float() function, it will result in ___.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.