Introduction
In Python, sometimes we need to change a string of numbers into a floating-point number, especially when we want to do math or calculations. Converting strings to float values is a common operation, especially when working with user inputs, data processing, or file operations. A string is simply text, and a float is a number with decimals. If you have a number as a string, you can easily turn it into a float using Python’s built-in tools.

In this article, you will learn how to convert a string to a float in Python. We will cover the simple way to do it using the float() function and how to avoid common errors that can happen during this conversion.
Converting String to Float
The most simple way to convert a string to a float in Python is by using the float() function. This built-in function accepts a string containing a numerical value and converts it into a floating-point number.
Example 1: Basic Conversion
# Example of string to float conversion
num_str = "12.34"
num_float = float(num_str)
print("String:", num_str)
print("Float:", num_float)
Output:
String: 12.34
Float: 12.34
In this example, the string "12.34" is successfully converted into a floating-point number 12.34.
Explanation
The float() function checks whether the string contains a valid numeric representation. If valid, it converts the string into a float. If not, it raises a ValueError.
Converting a string with commas
Sometimes, you may encounter strings that represent numbers with commas as thousand separators, such as "1,000" or "3,141,592.65". To convert these strings to floats, you need to remove the commas first. For example:
string_with_commas = "1,000,000.50"
string_without_commas = string_with_commas.replace(",", "")
float_num = float(string_without_commas)
print(float_num)
Output:
1000000.5
In this example, we start with a string "1,000,000.50" that contains commas. We use the replace() method to remove the commas by replacing them with an empty string "". The resulting string "1000000.50" is then passed to the float() function for conversion.
Let’s take a look at another example:
string_with_commas = "3,141,592.65"
float_num = float(string_with_commas.replace(",", ""))
print(float_num)
Output:
3141592.65
In this case, we directly pass the result of string_with_commas.replace(",", "") to the float() function, which removes the commas & converts the string to a float in a single line.
Note: It's important to keep in mind that this approach assumes the commas are used as thousand separators & the string follows a valid number format. If the string contains commas used for other purposes or has an invalid format, you may need additional processing or error handling.
Converting to a float list
In some cases, you may have a string that contains multiple numbers separated by spaces or other delimiters. To convert such a string to a list of floats, you can use a combination of string splitting and list comprehension. For example:
string_nums = "3.14 2.71 1.41 9.81"
float_list = [float(num) for num in string_nums.split()]
print(float_list)
Output:
[3.14, 2.71, 1.41, 9.81]
In this example, we have a string "3.14 2.71 1.41 9.81" that contains multiple numbers separated by spaces. We use the split() method without any arguments to split the string into a list of substrings based on whitespace. This gives us a list of strings: ["3.14", "2.71", "1.41", "9.81"].
Next, we use a list comprehension [float(num) for num in string_nums.split()] to iterate over each substring num in the split list and convert it to a float using the float() function. The resulting list of floats is assigned to the float_list variable.
You can also use a different delimiter to split the string. For example:
string_nums = "3.14,2.71,1.41,9.81"
float_list = [float(num) for num in string_nums.split(",")]
print(float_list)
Output:
[3.14, 2.71, 1.41, 9.81]
In this case, the string "3.14,2.71,1.41,9.81" uses commas as the delimiter. We pass "," as an argument to the split() method to split the string based on commas, and then convert each substring to a float using list comprehension.
Converting a list of strings to float
If you have a list of strings where each string represents a number, you can convert the entire list to a list of floats using the map() function. For example:
string_list = ["3.14", "2.71", "1.41", "9.81"]
float_list = list(map(float, string_list))
print(float_list)
Output:
[3.14, 2.71, 1.41, 9.81]
In this example, we have a list of strings string_list where each string represents a floating-point number. We use the map() function to apply the float() function to each element of the string_list. The map() function returns a map object, which we convert to a list using the list() function. The resulting list of floats is assigned to the float_list variable.
Let’s discuss another example that combines splitting a string and converting to floats:
string_nums = "3.14 2.71 1.41 9.81"
float_list = list(map(float, string_nums.split()))
print(float_list)
Output:
[3.14, 2.71, 1.41, 9.81]
In this case, we have a string string_nums containing numbers separated by spaces. We split the string using the split() method and then use map() to convert each resulting substring to a float. Finally, we convert the map object to a list using list().
Note: map() is a concise and efficient way to convert a list of strings to a list of floats. It applies the float() function to each element of the list without the need for an explicit loop.



