We cannot use mathematical operations on a string in Python programming. To do so, we have to **convert a string containing numeric value into a float or any number**. Most of the time we get data through text field or by reading a text file. Usually, entered data is received as a string. For doing mathematical or statistical calculations, we have to convert or parse the string (“numeric value in a string”) to a numeric value such as float.

Look at the following example for more understanding.

Example # 1

1 2 3 4 5 | # Declare a variable str = "555.55" # Now parse/convert a string into float. print(float(str)) |

Output:

`555.55`

Example #2

1 2 3 4 5 6 7 8 9 10 11 | # Declare a variables str1 = "260.888" str2 = "360.888" # str1 and str2 both are string # to perform numeric calculation we have to convert them # in a numeric such as float. # Adding two strings after converting into floats. print(float(str1) + float(str2)) |

Output:

`621.776`

Loading...