Tutorials  /  Python

How To Convert a String to a Float in Python

Ccentron Redaktion · January 2025 ·4 min read ·Python, Tutorial

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.

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

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

Prerequisites to Convert a String

In order to complete this tutorial, you will need:

  • Familiarity with installing Python 3.
  • Familiarity with coding in Python. How to Code in Python 3 series or using VS Code for Python.
  • 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 the specified object’s __float__() function.

Example

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

Python
input_1 = '10.5674'

input_1 = float(input_1)

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

Output:

Code
<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.

Python
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 the f prefix, please read f-strings in Python.

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

Code
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:

Python
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:

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 for Convert a String

Check out the complete Python script and more Python examples in our tutorials.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren