Tutorial

How To Convert a String to a Float in Python

Updated on September 22, 2022
author

Pankaj

How To Convert a String to a Float in Python

Introduction

In this article, we will use Python’s float() function to convert strings to floats. And we will also use Python’s str() function to convert floats to strings.

It is important to properly convert the data types before using them for calculations and concatenations in order to prevent runtime errors.

Prerequisites

In order to complete this tutorial, you will need:

This tutorial was tested with Python 3.9.6.

Using the float() function

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

Example

Let’s look at an example of converting a string to float in Python:

input_1 = '10.5674'

input_1 = float(input_1)

print(type(input_1))
print('Float Value =', input_1)

Output:

<class 'float'>
Float Value = 10.5674

The string value of '10.5674' has been converted to a float value of 10.5674.

Why do we need to convert a string to float?

If we are receiving float value from user input through the terminal or reading it from a file, then they are string objects. We have to explicitly convert them to float so that we can perform necessary operations on them, such as addition, multiplication, etc.

input_1 = input('Please enter first floating point value:\n')
input_1 = float(input_1)

input_2 = input('Please enter second floating point value:\n')
input_2 = float(input_2)

print(f'Sum of {input_1} and {input_2} is {input_1+input_2}')

Note: If you are not familiar with string formatting using f prefix, please read f-strings in Python.

Let’s run this code and provide float values for input_1 and input_2:

Please enter first floating point value:
10.234
Please enter second floating point value:
2.456
Sum of 10.234 and 2.456 is 12.69

The resulting sum of 10.234 and 2.456 is 12.69.

Ideally, we should use a try-except block to catch exceptions in case of invalid input from the user.

Using the str() function

We can also convert a float to a string using the str() function. This might be required in situations where we want to concatenate float values.

Example

Let’s look at an example:

input_1 = 10.23
input_2 = 20.34
input_3 = 30.45

# using f-string from Python 3.6+, change to format() for older versions
print(f'Concatenation of {input_1} and {input_2} is {str(input_1) + str(input_2)}')
print(f'CSV from {input_1}, {input_2} and {input_3}:\n{str(input_1)},{str(input_2)},{str(input_3)}')
print(f'CSV from {input_1}, {input_2} and {input_3}:\n{", ".join([str(input_1),str(input_2),str(input_3)])}')

Let’s run this code:

Concatenation of 10.23 and 20.34 is 10.2320.34
CSV from 10.23, 20.34 and 30.45:
10.23,20.34,30.45
CSV from 10.23, 20.34 and 30.45:
10.23, 20.34, 30.45

Concatenating 10.23 and 20.34 produces the string '10.2320.34'. This code also produces two versions of comma-separated values (CSV).

If we don’t convert float to string in the above program, the join() function will throw an exception. Also, we won’t be able to use the + operator to concatenate as it will add the floating point numbers.

Conclusion

You can check out the complete Python script and more Python examples from our GitHub repository.

References:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.