Updated April 1, 2023
Introduction to Python String to Float
The following article provides an outline for Python String to Float. In Python all the values we use or store as a variable will have a unique data type. It explains the nature of the value and depending on that Python automatically allocates a data type for that value and it helps in defining the kind of operation that is allowed to be done and stored in a particular structure bypass it to the compiler. And the Data types are mutable and for specific operations, the user can change the data types from float to int or string and vice-versa.
Syntax
Python has different data types for a different set of values, Integers deals with numbers and float deals with both decimal and numeric characters, Boolean deals with Binary values ( True or False) and there are strings that could take alphanumeric values and python allows different data structures like list, Tuple, Dictionary & sets for working with different problems. In the below example, we have declared 4 different values and checked the data types of the values to give an idea of the python data types.
Code:
a=44
b=20.35
c=2+4j
d=False
print(type(a))
print(type(b))
print(type(c))
print(type(d))
Output:
How to Convert String to Float in Python?
Let us see the functions used in the string to float conversion and vice versa.
Code:
float() function
This function returns a floating-point value from a string or a number.
Syntax:
float(argument)
Only one parameter can be used in this method and is optional.
There are two types of argument that can be used in this function.
- Number: The number can be any floating-point number or an integer.
- String: A string must contain numbers of any kind.
This method ignores any right or left white spaces.
Nan or inf can be used.
We can use mathematical operators as well.
Examples of Python String to Float
Given below are the examples mentioned:
Example #1
Let us see a simple example to print the string value of a given variable.
Code:
#code to print string value
print("Its a beautiful day")
print('Its a beautiful day')
a = 'Its a beautiful day'
print(a)
Output:
In the above code, we have printed the statement “Its a beautiful day” which is a string in itself and also we have assigned it to a variable ‘a’ which is also a string and many operations like slicing, replacing, concatenation, etc. can be done with string data type.
Example #2
Let us discuss another example as shown below.
#code to print string value
a = 'Its a beautiful day'
print(a[1])
print(a[2:7])
Output:
In the above code, we have declared the string ‘Its a beautiful day to the variable a. In the next line, we have printed the sliced 1st position of the string which is also a string. In the next line, we have sliced the 2nd and 7th positions in the string and printed it. String data type has versatile features and is easy to work with.
Example #3
In the next example, we will see how to use a string data type to convert them into float value.
Code:
#type conversion from string to float
x = '44.5610'
f = float(x)
print(type(f))
print('Float Value =', f)
Output:
In the above example, we have assigned the variable x and given it a float value inside quotes so that the compiler identifies it as a string value and in the variable f we have converted the string ‘x’ into float using python float() function.
Example #4
In this example, we will see how to convert the float values into string value.
Code:
#code to convert float to string value
x = 44.5610
s = str(x)
print(type(s))
print('string Value =', s)
Output:
In the above code, we have used the same example as above and removed the quotes to make the variable ‘x’ as float since it has decimal point the compiler identifies it as float value and passed it to the variable ‘s’ to convert it into string using the python str() function.
Example #5
In the below example we have got two string values as input and made an addition operation and printed the data type of the resulting value.
Code:
#code to sum of two input strings and convert it to float value
x = input('Enter first floating point value:\n')
x=float(x)
y = input('Enter second floating point value:\n')
y=float(y)
sum_inputs=x+y
print('sum of Inputs:{}'.format(sum_inputs))
print(type(sum_inputs))
Output:
In the above code, we have declared two input x and y as string and performed a summation operation, and converted them to float values and the resulting sum is printed as output. If we do not convert the values into float and perform addition operation, we would get the resulting output.
In this output above we have declared two string values and performed a sum operation, since they are string values the output is just a combination of both the strings mathematical operation is not performed since its a string value.
Example #6
In this example, we will see the arithmetic summation operation on two lists of float values that have been converted to string values
Code:
#code to add two list of float values and convert it to string value
x = [4.8, 5.6, 13.2, 45.4, 41.0]
y = [3.4, 7.7, 41.2, 5.3, 61.1]
sum_input=str(x+y)
print('sum of two lists:{}'.format(sum_input))
print(type(sum_input))
Output:
As done in the previous example we have declared two lists of float values and performed summation operation and converted the list to string using python str() function and we can see the output as a joining of the two lists together we can also use python join() function to get the same output.
Conclusion
In this article, we have discussed python string to float conversion using various examples. The data type conversion is considered to be very important in python since it is used in various real-time scenarios. When working with different kinds of algorithms in the data science field we need to convert data types.
Recommended Articles
This is a guide to Python String to Float. Here we discuss the introduction and how to convert string to float in python along with examples. You may also have a look at the following articles to learn more –