Convert String to Float in Python Last Updated : 26 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The goal of converting a string to a float in Python is to ensure that numeric text, such as "33.28", can be used in mathematical operations. For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore different methods to do this efficiently.Using float()float() function is an efficient way to convert a string into a floating-point number. As a built-in function, it returns a value of type float and works best with well-formatted numeric strings. However, if the input string is not a valid representation of a float, it raises a ValueError. Python a = "33.28" b = float(a) print(type(b), b) Output<class 'float'> 33.28 Using decimal.DecimalDecimal class from Python’s decimal module provides precise decimal arithmetic, making it ideal for situations where accuracy is important , such as financial calculations. It converts a string into a Decimal object, effectively avoiding common floating-point rounding errors. Python from decimal import Decimal a = "33.28" b = float(Decimal(a)) print(type(b), b) Output<class 'float'> 33.28 Using eval()eval() function reads and runs a string as a Python expression. It can turn a numeric string like "33.28" into a float. However, it's risky to use with untrusted input because it can run harmful code. It's best used only in safe and controlled situations. Python a = "33.28" b = eval(a) print(type(b), b) Output<class 'float'> 33.28 Using ast.literal_eval()ast.literal_eval() function safely evaluates strings that contain Python literals, such as numbers or lists. It can securely convert a numeric string to a float. Unlike eval(), it doesn’t run any code, making it a safer option for handling user input. Python import ast a = "33.28" b = ast.literal_eval(a) print(type(b), b) Output<class 'float'> 33.28 Related Articles:float() in Pythoneval in Pythonast.literal_eval() in Python Comment More infoAdvertise with us Next Article Convert String to Float in Python D deepanshumehra1410 Follow Improve Article Tags : Python python-basics Practice Tags : python Similar Reads Convert hex string to float in Python Converting a hex string to a float in Python involves a few steps since Python does not have a direct method to convert a hexadecimal string representing a float directly to a float. Typically, a hexadecimal string is first converted to its binary representation, and then this binary representation 3 min read Cannot Convert String To Float in Python Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con 3 min read Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver 3 min read Convert String with Comma To Float in Python When working with data in Python, it's not uncommon to encounter numeric values formatted with a mix of commas and dots as separators. Converting such strings to float is a common task, and Python offers several simple methods to achieve this. In this article, we will explore five generally used met 3 min read Convert String to Double in Python3 Given a string and our task is to convert it in double. Since double datatype allows a number to have non -integer values. So conversion of string to double is the same as the conversion of string to float This can be implemented in these two ways 1) Using float() method Python3 str1 = "9.02 2 min read Convert Decimal to String in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting decimal to string. Converting Decimal to String str() method can be used to convert decimal to string in Python. Syntax: str(object, encoding=âut 1 min read Convert Python String to Float datatype Let us see how to convert a string object into a float object. We can do this by using these functions : float() decimal() Method 1: Using float() python3 # declaring a string str1 = "9.02" print("The initial string : " + str1) print(type(str1)) # converting into float str2 = flo 2 min read How to convert Float to Int in Python? In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4 5 min read Convert Hex String To Integer in Python Hexadecimal representation is commonly used in computer science and programming, especially when dealing with low-level operations or data encoding. In Python, converting a hex string to an integer is a frequent operation, and developers have multiple approaches at their disposal to achieve this tas 2 min read How to convert string to integer in Python? In Python, a string can be converted into an integer using the following methods : Method 1: Using built-in int() function: If your string contains a decimal integer and you wish to convert it into an int, in that case, pass your string to int() function and it will convert your string into an equiv 3 min read Like