How to convert a string to a float in Python
In this post, you will learn how to convert a string to a float in the Python programming language. Python provides in-built type conversion functions, and by using them we can directly convert one data type to another. We don't have to import any additional modules, and we needn't bother with any mind-boggling methodology to do it.
Generally, we need to convert a string to float while performing arithmetic operations like addition, subtraction, multiplication, and so on. If we are getting user input or reading a file, those can be string objects. So, we need to convert them to float so that we can perform the necessary operations.
The float() function of Python is used to convert an object to a floating point number. This converts any datatype to a floating point number.
Syntax of float() function
float(object)
Here, the object can be an int, float, or string. It returns a float object. It will provide in the following condition:
- If the input string contains something besides a floating-point representation of a number, then it will raise a ValueError.
- If the given argument is outside the range of float, it will raise an overflow Error.
- If no argument is provided, then it will return 0.0
Python Convert String to float
The following example demonstrates how to convert a string to a float in Python.
str_input = "21.73"
print('String value =',str_input)
print('Type of float value =',type(str_input))
# converting string to float
float_num = float(str_input)
print('Float value =',float_num)
print('Type of float value =',type(float_num))
Output of the above code:
String value = 21.73
Type of float value = <class 'str'>
Float value = 21.73
Type of float value = <class 'float'>
Python Convert String with comma to float
If a string contains some extra commas, then we cannot directly pass this to the float() function. It will raise an error, for example:
str_input = "21,103.73"
# converting string to float
float_num = float(str_input)
Output of the above code:
float_num = float(str_input)
ValueError: could not convert string to float: '21,103.73'
Here, the float raised an error as the string had characters other than digits. Thus, we need to eliminate every extra comma from the string prior to passing it to the float() function.
str_input = "21,103.73"
print('String value =',str_input)
print('Type of float value =',type(str_input))
# convert string with comma to float
float_num = float(str_input.replace(',', ''))
print('Float value =',float_num)
print('Type of float value =',type(float_num))
Output of the above code:
String value = 21,103.73
Type of float value = <class 'str'>
Float value = 21103.73
Type of float value = <class 'float'>
Related Articles
Convert a string to a float in Python
Casefold in Python
isalpha Python
Python split multiple delimiters
Replace multiple characters Python
Python multiline string
Alphabet pattern programs in Python
Python remove punctuation from string
Remove character from string Python
Count vowels in a string python
Count occurrences of each character in string Python
Convert string to list Python
Python split multiple delimiters
Python program to reverse a string
Permutation of string in Python
Remove spaces from string Python
splitlines in python
Python program to sort words in alphabetical order
Python split string by comma
Remove character from string Python
Check if two strings are anagrams Python